Free Hosting For .NET 6 Projects

A Quick, Easy and Free Way to Get a Project out into The world

Roko Kovač
4 min readJun 18, 2022

Update November 2022:

Starting November 28th, 2022, free Heroku Dynos, free Heroku Postgres, and free Heroku Data for Redis® will no longer be available.

😢

As the popular saying goes, “there is no such thing as a free lunch”. However, as it seems, there might just be a free snack, which is exactly what the Heroku free tier would be if it were a meal. Being completely free of charge and extremely easy to set up, it is the perfect choice for showing your tiny hobby project or a quickly whipped up proof of concept to the world.

Prerequisites

Before we start, you should have the following:

All of those shouldn’t take more than 5–10 minutes to set up. After you’ve downloaded and installed Docker and Heroku CLI and made a Heroku account, you’re pretty much set. I will assume you already have a working .NET 6 application since you’re reading this article, but in case you don’t, here’s a 5 minute Hello World tutorial from Microsoft.

Creating the Dockerfile

As you already might have guessed, we will be using Docker to deploy our app to Heroku. If you’re unfamiliar with Docker, in short, it’s a container tool that makes deployment much easier. It is widely used and extremely useful. If you want to know more, you can read up on it here.

Navigate to the root of your project and create a file named “Dockerfile”. This is what it should look like:

.NET 6.0 Dockerfile for deploying to Heroku

The first step (Build) of the Dockerfile uses the official .net 6.0 sdk image as the base to build the app and saves it in the “out” folder.

The second step (Run) uses the official .net 6.0 runtime image as the base, copies the previously built files and runs the compiled .net 6.0 dll. Obviously, you should replace “App” with the name of your project.

Take note of the last line. Before running the app, we’re setting the ASPNETCORE_URLS env variable to http://*:$PORT. This makes the framework bind to the provided url.

The $PORT env variable is set by Heroku and gives us the exposed port of the instance on which our app is hosted.

Deploying to Heroku

Now that we have the Dockerfile ready, you can open up a shell in the root of your project (where your Docker file is located).

First, you need to log into Heroku. To do that, execute the following command, which will open up the browser and allow you to log in.

heroku login

If you run into any issues, alternatively, you can use “heroku login -i”. Once you’ve logged in, execute the following commands:

heroku create myapp --region eu
heroku container:login
heroku container:push -a myapp web
heroku container:release -a myapp web

First, we create the heroku app. Note that the name of the app must be unique and available, so you will need to use something other than “myapp”. Regarding the region flag, you can choose between “eu” and “us”.

Then, we log into the Container Registry, build the Docker container using the Dockerfile we created, push it to Heroku, and finally, deploy it.

If everything goes well, your app should be up and running. You can open it in your browser using the following command:

heroku open -a myapp

Adding a Database

Your app will probably need a database. Luckily, with Heroku, it’s really easy to add a database and link it to your app. There are many free options to choose from. In this tutorial, we will be using the Heroku Postgres hobby-dev plan. To add it to your Heroku app, run this command:

heroku addons:create heroku-postgresql:hobby-dev -a myapp

When you add the Postgres addon, the connected Heroku instance will have a preset environment variable called DATABASE_URL, which contains the url to access the database. This is what it looks like:

postgres://{user}:{password}@{hostname}:{port}/{database-name}

For your application, you will probably need a standard Postgres connection string. We can parse the url into a connection string using this nifty method:

If you’re using Entity Framework, you could have an extension method that looks something like this:

Which you would then call when configuring your sevices, as such:

builder.Services.SetupAndAddDbContext(builder.Configuration);

Conclusion

I have shown you how to deploy your app, along with a database, easily, quickly and with zero cost, using Heroku.

Obviously, there are performance limitations to what the free heroku plan can handle, including the “cold start”, but I believe it’s only a minor inconvenience for a completely free, easy to set up service. Obviously, it’s not meant for production apps.

Have you run into any problems? Do you have any thoughts on Heroku? Let me know what you think.

--

--