Creating And Hosting ASP.NET Core Application On Linux

Introduction and Background

It has been a while since I wanted to write something about ASP.NET Core hosting stuff. Kestrel is another interesting topic from my own perspective. Previously, I have written a lot to cover the basics of ASP.NET Core programming. In this one, I will guide you through hosting the ASP.NET Core application natively, using .NET Core libraries that you download during the installation process.

Secondly, it has been a while since I have written anything at all and I might have forgotten the interests of my readers. So, excuse me if I miss a few things.

 Image captured from Scott Hanselman’s blog
Figure 1: Image captured from Scott Hanselman’s blog.

There is a lot of stuff to share in this post today. So, stay tuned and hold your breath. I will be covering the following steps in this article -
  1. Installation of the latest version of .NET Core on Linux environment

    • If you already have a .NET Core framework installed, upgrade your system if it doesn’t support ASP.NET programming as of now.
    • In the previous versions, web development wasn’t supported in .NET Core for Linux operating systems. In the recent versions, it has been added. That's why I insist that you at least install the latest version of .NET Core, before continuing.

  2. Setup an ASP.NET Web Application in your Linux environment

    • I will guide you through many stages, such as development, editing, maintenance, and using an IDE for all of these things.
    • I will walk you through many different areas (not the ASP.NET Area!) in ASP.NET web application’s file hierarchy. Plus, I will tell you what is back and what is about to change (or subject to change in the hierarchy).

  3. Build and run the project

    • In this section, I will guide you through a few of the tips that I  deem helpful to you.
    • Before running the project, I will give you an overview of Kestrel — the web server for ASP.NET 5.
    • I will, then, move onwards to using the website, which has the same feel as it had in the Windows operating system, while developing ASP.NET web applications.
    • Tip- I will give you a tip. By default, an application is uploaded at 5000 port which you would rneed to change in many cases. I will show you how to change the port of the web application’s hosting service.

  4. Final Words

    Finally, I will head over to final words that I put at the end of every post I write, on any of the platforms that I have to write about.

Installing or upgrading .NET Core

I wrote an article that covered how you can start using .NET Core on Linux environment. You can read it here. That article was written entirely, to give you a concept of the .NET Core architecture, and a few commands that you can use to do most of the stuff. The only difference between the older article and this section of the post is that this would just contain a later version of .NET Core to be installed on the system. Whereas in the previous one, you had to install the older one.

Try the following.

sudo apt-get update && dist-upgrade


If that doesn’t work or it doesn’t show the updates, head over to the main website for .NET Core, and install the latest version from there.

Creating a new web application

Previously, .NET Core only supported creating libraries or console applications. Currently, it supports ASP.NET web application development too;it alos supports the templates for ASP.NET. At the moment, just a simple ASP.NET MVC oriented web application is created, and I don’t think there is any need for other templates, like Web API, SignalR, single page applications etc. I believe that is complete at the moment.

To create a new web application, create a new project in .NET Core and pass the type parameter as a web application. This would guide .NET Core (dotnet) to create a new application with the template of web application.

Creation of a new .NET Core project using web template
Figure 2: Creation of a new .NET Core project using web template.

As you can see in the command above, we are passing a type (-t parameter) to the command, in order to create a new web templated application. The content is as follows.

ASP.NET Web application files and directories in .NET Core
Figure 3: ASP.NET Web application files and directories in .NET Core.

The project looks similar to what we have on Windows environment. Just a difference of one thing: It includes web.config as well as project.json file. Remember that Microsoft is mostly moving back to MSBuild, and of course, a few of the hosting modules are based on IIS servers. So, that is why web.config file is available in the project. However, the project at the moment uses project.json file for project-level configuration and (as we have heard mostly during these days) these would be migrated to the MSBuild way of managing the project and other related stuff.

Editing and updating the web application

Microsoft has been pushing the limits for awhile. Obviously, in Linux, you already had a few of the best tools for development, but Visual Studio Code is still a better tool. In my previous posts, I said that I don’t like it enough — I still don’t like it very much, it needs work! — but it is better than many tools and IDEs available for C# or .NET-related programming. Since in this post, I said that I won’t be talking about anything third-party, Visual Studio Code is the tool that I am going to suggest, support, and use in this case.

You can install Visual Studio Code from their official website. Note one thing - The Debian packages take a little longer for installation. So, if you also don’t like to wait too much like me, then I would recommend that you download the archive packages and install the IDE from those. There is no installation required at all. You just move the extracted files to a location where you want to store it. Finally, you create a symbolic link to your executable, which can be done like this.

sudo ls -s /home/username/Downloads/VSCode-linux-x64/code /usr/local/bin/code

This would create a link where you can use Visual Studio Code IDE, just by executing this “code” command anywhere in the directory (using the terminal). It would, then, load the directory itself as a project in the IDE, which can be used to update your application’s source code and other stuff that you want to.

Tip- Install the C# extension from marketplace for a greater experience.

Once this is done, head over to the directory where you created the project. Execute the following command - 

code .

This would trigger the Visual Studio Code to load your directory as the project in the IDE. This is how my environment looks.

Visual Studio Code showing the default ASP.NET directory as a project
Figure 4: Visual Studio Code showing the default ASP.NET directory as a project.

The rest is history — I mean, the rest is the similar effect and feeling that you can get on Windows environment. First of all, when you work this way and follow my lead, you will get the following errors in the IDE window.

Error messages in Visual Studio Code
Figure 5: Error messages in Visual Studio Code.

We will fix them in a moment. But first, understand that these are meant to act like this. If you have had ever programmed in .NET Core, you must have known that before anything else, you need to restore the NuGet packages for your project, even before building the project. Visual Studio Code uses the same IntelliSense and tries to tell you what is going wrong. In doing so, it requires the binaries which are not available. So, you are shown that last error message. The above two are optional while the middle one is a bit optional-required.

Build and run the project

So far, we have created the project. But now, we need to restore the dependencies for our project, which are going to be used by the .NET Core to actually execute our code. By default, a lock file is not generated. So, we need to create a new file and after that, we will be able to build the project.

Execute the following command.

dotnet restore

After this, .NET Core will automatically generate the lock file, and building process can be easily triggered.

Project.lock.json file created after restoring the project
Figure 6: Project.lock.json file created after restoring the project.

We can continue to building the project. I want you to pay attention to this point now. See what happens.

Build and run process of ASP.NET web application on .NET Core
Figure 7: Build and run process of ASP.NET web application on .NET Core.

Now, notice a few things in the above terminal window. First of all, notice that it keeps logging everything.

Secondly, notice that it has a port “5000” appended. We didn’t train it to use that at all, and that also didn’t come from Program.cs file either (you can see that file!). But before I dig any deeper into an explanation of how to change that, I want you to praise Kestrel — The web server of ASP.NET 5.
 
Kestrel is the implementation of libuv async I/O library that helps hosting the ASP.NET applications in a cross-platform environment. The documentation for Kestrel and the reference documentation is also available, and you can get started reading most of the documentation about Kestrel, on the namespace Microsoft.AspNetCore.Server.Kestrel, at the ASP.NET Core reference documentation website.

This is the interesting part because ASP.NET Core can run even on a minimal HTTP listener that can act as a web server. Kestrel is a new web server; it isn’t a full featured web server but adapts as community needs updates. It supports HTTP/1 only as of now but would support other features in the coming days. But remember, pushing every single feature and module on one server would actually kill the major purpose of using the .NET Core itself.
 
.NET Core isn’t developed to push everything on the stack, but instead, it is developed to use only the modules and features that are required. Yes, if you want to add a feature, you can update the code and build the service yourself. It is open sourced on GitHub.

Changing the port number of application

In many cases, you might want to change the port number where your server listens; or you may also want to make this server the default server for all of your HTTP based communication on the network. In such cases, you must be having a special port number assigned (80-for default).

For that, head over to your Program.cs file, the main program file (main function of your project), that is responsible for creating a hosting wrapper for your web application in ASP.NET Core environment. The current content for this object is displayed below.

 Source code for the Program.cs file
Figure 8: Source code for the Program.cs file.

In that chain of “Use” functions, we just need to add one more function call to use a special URL. The function of “UseUrls(“”)” would allow us to update the URLs that are to be used for this web application. So, I am just going to use that function here so that it would let us simply select which URL (and port) to use.

URLs being managed
FIgure 9: URLs being managed.

Now, if you try to run the application, you will run into the following problem in Linux-based system.

Unable to bind to the URL given, error message
Figure 10: Unable to bind to the URL given, error message.

That is pretty much simple — it doesn’t have permission to do the trick. So, what happens on Linux systems is that it is simply performed using the super user credentials and account.

sudo dotnet run

It would prompt you for your password. Enter your password and this time, application will run on the localhost:80. In the cases where you have to upload the website to the servers, Azure App Services etc., you are required to have the application running under the server that acts as the default server. So, in those cases, you must handle the default TCP port for HTTP communication for the requests. Otherwise, you might need to work at the backend of NGINX or Apache servers etc. But, that is a different story.

Web server running at port 80
Figure 11: Web server running at port 80.

Now that our server is running, we can go to a web browser to test it out. I am going to use Firefox but you can use any web browser (even a terminal-based one).

ASP.NET Core web application
Figure 12: ASP.NET Core web application being rendered in Firefox, running in Linux using Kestrel web server.

As you can see, the web application runs smoothly. Plus, it also logs any event that takes place. The terminal keeps a record of that. It can also allow you to actually transfer the output from main terminal’s output to a file stream, in order to store everything. But, that is a topic for a different post.

ASP.NET web application’s log in terminal window
Figure 13: ASP.NET web application’s log in terminal window.

As requests come and responses are generated, this window will have more and more content.

Final words

For a while, I wanted to write my own next web application for my own blog and stuff, in ASP.NET Core. At the moment, the framework is “almost” ready for production, but just not yet. Maybe in a couple of the next builds, it will be ready. There are many things that they need to look into. For example, the web server needs to be more agile — the features need to be provided. That is not it. VIsual Studio Code must be fully integrated with ASP.NET Core tooling. At the moment, it just allows us to edit the code. Then, we have to go back to the terminal to do the rest. It should provide us with the ability to do that.

To Microsoft’s team related to .NET Core

Why isn’t .NET Core being published on Linux? I am pretty sure the framework is fully functional, despite the bugs. But there are bugs in the main .NET framework too. There are some minor issues every now and then. My major concern here is to be able to do “sudo apt-get install dotnet”. I can’t remember the longer version names.

To readers

If you are willing to use ASP.NET Core for your production applications, wait for a while. Although ASP.NET Core is a better solution than many other solutions available, but my own recommendation is to stick to ASP.NET 4.6 as of now because that is a more stable version as compared to this one.

Up Next
    Ebook Download
    View all
    Learn
    View all