.NET Core 1.1 – How to publish a self-contained application


If you are new with .NET Core 1.1, 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,

Comments (15)

  1. mario says:

    I came here to look for how to run an application. I did the publish from VS2017 and i assume it did all this for me. But how do you actually run it?

    1. Hi Mario,
      There are some options to running your application.
      1) You can run from VS2017 by pressing F5 to debug you application.
      2) After publishing to IIS, for example, you can browse the URL http://localhost/ followed by the name of you application (virtual directory)
      3) You can open the cmd, navigate to you app solution folder and type:
      dotnet run
      After that, all you have to do is access you application through http://localhost:5000/ followed by the name of your application.

  2. Vinicius says:

    Quick question. Can I publish to Ubuntu or osx from a Windows machine and vice versa?

    1. Yes, All you have to do is publish informing the runtime (Win10-x64, osx.10.11-x64 or ubuntu.16.10-x64). After that, you need to use a tool of you preference to copy the files.

  3. Marcelo Salduendo says:

    Hi Luis,
    Thanks so much for this tutorial.. I’m doing my first steps on asp.net core and this was helpful for me
    Just a question, my publish process does not generate the exe file like yours..
    But, on web config the line seems to be Ok:

    (note that my project name is different)
    I can not figure it out.. what am I doing wrong?
    using VS 2017 Community
    Thanks in advance,
    Marcelo

    1. Thanks for your great feedback.
      I think that the easy way to do that is to open up your project file (.csproj) and include the following line inside the PropertyGroup tag:

      <RuntimeIdentifiers>win10-x64</RuntimeIdentifiers>

      After saving the changes, you need to restore the references of the project. You can run the following command:

      dotnet restore

      Finally, you can publish your self-contained application using the following command:

      dotnet publish -c release -r win10-x64

      Observe that I am using the win10-x64 runtime. Feel free to change that.

      The executable file is supposed to be at:

      \bin\release\netcoreapp1.1\win10-x64\publish

      I hope have helped.

      Regards,

      1. pesquera says:

        Hi Luis,
        The problem about not generating the exe file, was the uppercase
        Just in case, to help somebody else with the same issue:
        correct: dotnet publish -c release -r win10-x64
        incorrect: dotnet publish -c release -r Win10-x64
        Please correct the code above (with the screenshots)
        Best Regards,
        Marcelo

        1. Thank you so much for let me know about that. 🙂
          I will fix that immediately.

  4. Steve Bishop says:

    They really need to make it easier to select the runtime. These XML tags are so arbitrary and not documented at all in the CLI documentation. Thank you for explaining it.

  5. Syed Raza says:

    Hi Luís Henrique,

    Hope you will be very fine, I have one question regarding deploying it on hosting server through ftp. What actually published is exe file and I dont understand how can I deploy standalone package on server with IIS support.

    Regards
    Syed Raza

    1. Thanks for your question.
      In this case, all you have to do is copy the content where your application was published (i.e., dotnet -c realease -r win10-x64) to the IIS application virtual directory. In this case, the IIS will act as a reverse-proxy, where w3wp.exe process will call your application binary. Take a look inside the web.config (after published) to see how it works.
      I hope have helped.
      Regards

  6. Dan Mills says:

    Hi Luis,
    Thanks for all this.
    My question has to do with the very last paragraph.
    The machine I want to run the self-contained application on belongs to someone else, (godaddy)).
    I have a windows, shared-hosting account with them, and so I expect to use IIS by default.
    They have not installed anything for asp.net core on their servers.
    Is the ASP.NET Core Server Hosting Bundle something I need to install on my machine or does it need to go on the server?
    Thanks!
    Dan

  7. Dev says:

    Have you tried this Console app(.net core). It is not so easy as this… 🙁

    1. The steps is the same. Please, double-check that you are using the following command:
      dotnet publish -c release -r win10-x64

      The executable file will be at:
      \bin\release\netcoreapp1.1\win10-x64

Skip to main content