Deploy your Dotnet API to the Heroku

Sandun Isuru Niraj
5 min readAug 3, 2020

Hi Folks, Today, I’m going to talk about deploying a Dotnet core API to the Heroku. Heroku is one of the leading PaaS(Platform as a Service) provider that allows developers to build and deploy applications like APIs and Websites in a cloud environment. They provide lots of features like Pipeline configurations and continuous integration with automatic deployments and many more. Also, Heroku is a one-stop location for those who are going to learn about cloud infrastructure. They provide free computing capacity to everyone who has created an account. So you can use this fantastic tool to understand the cloud basics; also, they have several add-ons like MySQL, MsSQL to connect with your application, just a click ahead.

Cloud instances provided by Heroku are called as ‘Dyno’. By creating a free account with your email and confirming the email can enroll in a free dyno tier. Here you will get one dyno that means one cloud instances can share within your all applications inside your Heroku account. Here are some specifications for your free dyno.

Memory — 512MB

Code and Assets size — 500MB

Maximum Processes — 256

Bandwidth Limit — 2TB/Month

Process Time — 750Hours/Month

After seeing this, you will realize, This is way better for a learner. So then we move into our main topic.

In Heroku, It is not supported for the Dotnet core applications directly. But Heroku also just another cloud computing service. We can do a small hack to that because Heroku is supplying some custom build pack feature to add your build packs apart from the Heroku’s readymade build configurations. This feature is beneficial when you are going with complex applications hosted inside the Heroku. So we can use that same feature to deploy our Dotnet application to the Heroku.

First, we need to create a Heroku account and confirm the email. Then need to create a Project in Heroku. You can log in to the Heroku Dashboard using this link.

Then create a New Project by clicking on the New Button.

Heroku Dashboard

And click on Create new app.

Create a new App

Then give a proper name. Remember, the name you have provided here will also be your API URL along with .herokuapp.com. So choose wisely. Also, this name needs to be unique.

Heroku Dashboard — Overview

Then you will be redirected to a page like this and, now we need to add our custom build packs for the Dotnet. So for that move to the settings tab. And Find the Buildpacks section there.

Heroku Dashboard — Buildpacks

Initially, you will not see any build packs here. Because currently, it is using Heroku’s readymade Build configurations. These readymade configurations are presently available for NodeJS, Python, PHP, Ruby, and some other only.

Heroku readymade supported Languages and Technologies.

So now, we need to add 3rd party Buildpack that can use to deploy a Dotnet core application. So I’m using this build pack mentioned below.

So now click on the Add buildpack button and paste this on buildpack URL.

jincod/dotnetcore

Then click on Save changes. Now your build configurations for Dotnet core is enabled.

So for the demo, I have a Dotnet core sample API.

Dotnet core Sample API

This has a sample API endpoint that outputs pre-defined weather values when we call the endpoint. So I’m going to host this API in the Heroku.

First, we need to download Heroku CLI for the deployments. You can download and install it from the link below.

Then we need to login to our Heroku account first with Heroku CLI. Type ‘Heroku login’ and it will spawn a webpage, and you need to login to the Heroku account. Also, you need a git installed in your pc too.

In the Project’s root directory, open your command prompt.

Project’s root directory

Then create a git repository by running ‘git init’ command.

All commands can find in the deploy tab in your Heroku Dashboard.

Heroku Dashboard — Deploy

Now configure the Heroku remote repository to the git repo by running the command, ‘heroku git:remote -a <Your Project Name>’. In this example, my project name is ‘myweatherapplk’. Then add your project files into the repo by running ‘git add .’ command, and add a commit message as usual. Now push the entire git repo to the remote Heroku directory by running, ‘git push heroku master’ command.

Heroku CLI build

Now, you can see our API is built inside the Heroku server according to our custom buildpack configurations. Wait until it finishes after the deployment. You can see the API base URL in the CLI as below.

API Base URL

Now you can use this API URL to call your API.

API response

You can see it is now on live and responds to our requests.

I think this will be helpful to you guys if you are new to the Cloud and PaaS. In the next few articles, I will talk about how we can extend the abilities of these APIs by connecting Databases with them using Heroku add-ons.

Stay Safe! Cheers!

--

--