Creating your first .NET Core project on Linux

And configuring Kestrel for a web environment

This post is the first of a series in which we’ll discuss about the experience of developing and running .NET web applications in Linux environments.

Introduction to .NET Core

.NET Core is an opensource, multiplatform version the .NET Framework which is officially supported and maintained by Microsoft.

This post will give an overview of how to run a .NET Core web project in Ubuntu, starting with its basic architecture and ending in the configuration of Kestrel for creating a web application.

.NET Core framework already provides scaffolding tools for bootstrapping your projects, but this time we’ll develop this from scratch starting from a basic console application.

Installing .NET Core on Linux

We’ll install .NET Core in Ubuntu, following Microsoft’s official website, but you can find installation instructions for most common distros. As a development IDE you can use any text editor, but we are going to use Visual Studio Code which, in my opinion, offers the best integration.

Creating my first “Hello world” in C # .NET Core

From the command console run these commands:

  • dotnet new console (to create a console project in .NET Core)
  • dotnet restore (to restore packages and project dependencies)
  • dotnet run (to execute our code).

Configuring Kestrel

Kestrel is a multi-platform web server used to host .NET Core web applications.

To add Kestrel to our project we must add the it’s dependencies to the .csproj file as follows:

Now we will open our Program.cs file and in it’s Main method replace the following lines:

For these lines:

In order to use Kestrel and the HTTP context we will have to add these references:

Now let’s run dotnet restore and dotnet run to test our project in our browser.

Using Startup.cs

Now we will create the file Startup.cs, which will have to configure for the project host.

For this to happen we are going to remove the following line from Program.cs:

And let’s add this line in its place:

By now our Startup.cs should be looking something like this:

The Configure method is called at runtime and it is used to configure and channel HTTP requests.

IApplicationBuilder provides the mechanisms for configuring application requests. The middleware is also configured here.

Next, we will configure the app to use IIS/IIS Express for the web server and set the content root. To do this we will add the following lines before the Build() in Program.cs:

To use IIS with ASP.NET Core, both UseKestrel and UseIISIntegration must be specified. Kestrel is designed to run behind a proxy and should not be deployed without it.

Now let’s add the IIS package reference in .csproj, and run dotnet restore for updating dependencies.

We will add “Watcher” to see the changes reflected without needing to stop and re-run the project. For that we are going to add the following dependency:

As always we will have to run dotnet restore, and now if we run dotnet watch run we should be able to see our changes being reflected without us having to reload out application.

Congratulations!

You already have your .Net Core application running on Linux and you learned how to configure it from scratch using Kestrel and develop it in a web environment. In the next post, I explain how to build an MVC architecture application using the tools provided by .Net Core.