Udostępnij za pośrednictwem


.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,

Comments

  • Anonymous
    March 29, 2017
    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?
    • Anonymous
      April 02, 2017
      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 runAfter that, all you have to do is access you application through http://localhost:5000/ followed by the name of your application.
      • Anonymous
        July 17, 2018
        HI Luís Henrique Demetrio,how could i change the port number from 5000 to any other like 8089
        • Anonymous
          July 19, 2018
          You can use the UseUrls method of WebHostBuilder or you can configure the server address using the configuration.Please check the following documentation for more information:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/web-host?view=aspnetcore-2.1#server-urls
  • Anonymous
    March 30, 2017
    Quick question. Can I publish to Ubuntu or osx from a Windows machine and vice versa?
    • Anonymous
      April 02, 2017
      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.
  • Anonymous
    May 06, 2017
    The comment has been removed
    • Anonymous
      May 07, 2017
      The comment has been removed
      • Anonymous
        May 10, 2017
        Hi Luis, The problem about not generating the exe file, was the uppercaseJust in case, to help somebody else with the same issue:correct: dotnet publish -c release -r win10-x64incorrect: dotnet publish -c release -r Win10-x64Please correct the code above (with the screenshots)Best Regards,Marcelo
        • Anonymous
          May 10, 2017
          Thank you so much for let me know about that. :)I will fix that immediately.
  • Anonymous
    May 09, 2017
    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.
  • Anonymous
    May 14, 2017
    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.RegardsSyed Raza
    • Anonymous
      May 14, 2017
      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
  • Anonymous
    May 24, 2017
    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
    • Anonymous
      May 25, 2017
      It's on the server.
  • Anonymous
    May 25, 2017
    Have you tried this Console app(.net core). It is not so easy as this... :(
    • Anonymous
      May 25, 2017
      The steps is the same. Please, double-check that you are using the following command:dotnet publish -c release -r win10-x64The executable file will be at:\bin\release\netcoreapp1.1\win10-x64
  • Anonymous
    July 03, 2017
    I published to centos.7-x64 succesfully. Then how can I deploy it to the centos server with Nginx ? I uploaded all files to the server and run dotnet run (or dotnet run myapp) => Couldn't find a project to run. Ensure a project exists in /var/www/myapp. Or pass the path to the project using --project
    • Anonymous
      July 19, 2018
      Sorry the huge delay to answer you! After one year, I realized that I missed this question. I believe that you already have the answer, but I will post the link here anyway in case it can be useful to anyone else:https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/linux-nginx?view=aspnetcore-2.1&tabs=aspnetcore2x
  • Anonymous
    November 09, 2017
    As the identifier is w10-x64, if I understand well it means it will run on Windows 10 only, right? or not?What about if I would like that the generated application be able to run on Windows 8 and 7 as well?Thank you
    • Anonymous
      November 09, 2017
      The comment has been removed
  • Anonymous
    May 02, 2018
    Just have to say thank you! It really helped me a lot!
    • Anonymous
      July 19, 2018
      Thank you!
  • Anonymous
    July 05, 2018
    Hi,I have one question related to Self contain deployment.My project have two console applications project named "CosoleApp1" and "ConsoleApp2".When I publish project the relevant publish folder created inside individual project folder along with "ConsoleApp1.exe" and "ConsoleApp2.exe".So now my question is how to publish solution so all the executables for all project will remain in one common folder so we need to pick only that folder in Self contain package.Thanks,
  • Anonymous
    September 05, 2018
    Thank you Luis,i followed the instructions and it works like a charm on windows 10. but then i copied the files to the windows server 2008, and it says: “-ms-win-crt-runtime-l1-1-0.dll was not found”. i suspect if .net core is not compitable for windows server 2008?
    • Anonymous
      September 11, 2018
      Hi Ali.If you published as self-hosted you don't need to install anything and it will work. But if it is an ASP.NET Core App and if you are trying to host on IIS, you will need to install the ASP.NET Core Server Hosting Bundle on the server that is available at https://go.microsoft.com/fwlink/?linkid=837808.Let me know if it helped.
  • Anonymous
    November 23, 2018
    hi , i published succesfully but the same time routing static files in wwwroot folder get 404 not found statuse code !not : I have been Added app.UseStaticFiles();
    • Anonymous
      November 23, 2018
      The comment has been removed
      • Anonymous
        November 23, 2018
        Thanks for your answer, You were right I was making a mistake, I have been added App.useStaticFiles() after app.useMvc ()another question : how can I Change Default Startup URL Address(https://localhost:5001) and bind to my server IP Address
        • Anonymous
          November 23, 2018
          before mark it as spam , i have been reading your another answer ,thanks
          • Anonymous
            November 23, 2018
            I am glad that it worked.Please let me know if the following post helps you about the URL question:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/web-host?view=aspnetcore-2.1#server-urls
          • Anonymous
            November 26, 2018
            Hi againPlease forgive me for the weakness in EnglishThat's awesome, open the specific port in the firewall and call the program with ip addreess, but many times trying to run the application, the program crashed, I finally found the problems:The program may crash in the following cases:Listen to a port like 80.Use HTTPS on UseUrls (....)here , my first selef-containd .net core web App, thanks to microsft :http://185.2.14.88:5000/
  • Anonymous
    March 18, 2019
    The comment has been removed
    • Anonymous
      May 16, 2019
      The comment has been removed
  • Anonymous
    April 17, 2019
    The comment has been removed