.NET Core 2.0 - How to publish a self-contained application

If you are new with .NET Core 2.0, please take a look at the post .NET Core 1.1 – Where to Start first.

 

I am assuming you already have an ASP.NET MVC application created and build. If you don’t have, please check the following post: NET Core 1.1 – Creating an ASP.NET Core using the .NET CLI for details about how to create an ASP.NET Core MVC application.

 

The idea of this post is to show you how to create a self-contained application that does not requires that the .NET SDK be installed on the target machine.

 

The first thing that we need to do is to edit the .csproj file, in my case the HelloWorld.csproj, to include the runtimes. By default, the .csproj is created as follows:

 

csproj

 

If I want to publish a self-contained version of my application to Windows, for example, all I have to do is to modify the .csproj including the following line:

 

 
<RuntimeIdentifiers>win10-x64</RuntimeIdentifiers>

csprojsc

 

After saved the changes, it is necessary to run the following command to restore the dependencies:

dotnet restore

 

To generate the self-contained application, run the following command:

dotnet publish -c release -r win10-x64

 

publishwin10

 

 

As I am using the runtime Win10-x64, the outcome files will be generated inside the folder win10-x64\publish:

 

scfiles

 

 

Observe that this time, the application was build with the .exe extension instead of .dll. If you open up the web.config of this publish folder, you will notice that this file does not refers to the dotnet.exe anymore:

 

scwebconfig

 

 

Now, lets suppose that you would like to publish to MAC or Linux. In this case, all you need to do is to add the following runtime to the .csproj file:

<RuntimeIdentifiers>win10-x64;osx.10.11-x64;ubuntu.16.10-x64</RuntimeIdentifiers>

 

And restore again the dependencies of your project:

 

dotnet restore

 

Also, you need to inform the runtime target during the publish:

  • dotnet publish -c release -r ubuntu.16.10-x64

 

linux

 

  • dotnet publish -c release -r osx.10.11-x64

osx

 

 

If the machine that you want to run the self-contained application does not have the .NET Runtime, and if you decided to use IIS to host your application, in this case like a reverse-proxy, you have to install the .NET Core Module for IIS that is available at ASP.NET Core Server Hosting Bundle.

 

I hope you liked it.

Regards,