Publishing your applications

There are two ways to publish and deploy a .NET Core application:

  • Framework-dependent
  • Self-contained

If you choose to deploy your application and its dependencies, but not .NET Core itself, then you rely on .NET Core already being on the target computer. This works well for web applications deployed to a server because .NET Core and lots of other web applications are likely already on the server.

Sometimes, you want to be able to give someone a USB key containing your application and know that it can execute on their computer. You want to perform a self-contained deployment. The size of the deployment files will be larger, but I will know that it will just work.

Creating a console application to publish

Add a new console application project named Ch16_DotNetCoreEverywhere.

Modify the code to look like this:

    using System; 
 
    namespace Ch16_DotNetCoreEverywhere 
    { 
      class Program 
      { 
        static void Main(string[] args) 
        { 
          Console.WriteLine("I can run everywhere!"); 
        } 
      } 
    } 

Open Ch16_DotNetCoreEverywhere.csproj, and add the runtime identifiers to target four operating systems, as shown in the following markup:

<RuntimeIdentifiers>win10-x64;osx.10.12-x64;rhel.7-x64;ubuntu.14.04-x64</RuntimeIdentifiers> 

Note

The RID value win10-64 means Windows 10 or Windows Server 2016. The RID value osx.10.12-x64 means macOS Sierra. You can find the full list of currently supported Runtime IDentifier (RID) values at the following link: https://docs.microsoft.com/en-us/dotnet/articles/core/rid-catalog

Publishing with Visual Studio 2017

In Visual Studio 2017, right-click Ch16_DotNetCoreEverywhere, and choose Publish..., and then click Publish, as shown in the following screenshot:

Publishing with Visual Studio 2017

You have now published the Windows version, as shown in the following screenshot:

Publishing with Visual Studio 2017

Click Settings, and change the Target Runtime to osx.10.12-x64, as shown in the following screenshot, and then click Save:

Publishing with Visual Studio 2017

Click Publish.

In Solution Explorer, show all files, expand bin, Release, netcoreapp1.1, osx.10.12-x64, and win10-x64, as shown in the following screenshot, and note the application files:

Publishing with Visual Studio 2017

You can repeat these instructions for the other two operating systems.

Publishing with Visual Studio Code

In Visual Studio Code, in Terminal, enter the following commands to build release versions for Windows 10, macOS, Red Hat Enterprise Linux, and Ubuntu Linux:

dotnet restore
dotnet publish -c Release -r win10-x64
dotnet publish -c Release -r osx.10.12-x64
dotnet publish -c Release -r rhel.7-x64
dotnet publish -c Release -r ubuntu.14.04-x64

Open a Finder window, navigate to Ch16_DotNetCoreEverywhereinRelease etcoreapp1.1, and note the output folders for the four operating systems, and the files, including a Windows executable named Ch16_DotNetCoreEverywhere.exe, as shown in the following screenshot:

Publishing with Visual Studio Code

If you copy any of those folders to the appropriate operating system, the console application will run because it is a self-contained deployable .NET Core application.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.178.9