Running Self-contained ASP .NET core application o

2020-01-28 07:31发布

问题:

First, I'm a Linux newbie so forgive me.

I've published an ASP .NET Core application as a self-contained app targeting Ubuntu. The publish seems to work fine. I've copied the files to a pretty vanilla Ubuntu machine. Now, how do i run my application? My understanding is that because it is a self-contained .NET Core application I DO NOT need to download and install .NET Core anything. My app should contain everything it needs.

All tutorials seem to say i should call $dotnet run. However the "dotnet" command line doesn't exist (is it supposed to be published into the self-contained folder??) So if i call it, i get "command not found". Of course i could download .NET Core, but doesn't that go against the whole self-contained concept? Here is a sample of the files i'm copying over.

回答1:

Answer

Now, how do I run my application? My understanding is that because it is a self-contained .NET Core application I DO NOT need to download and install .NET Core anything. My app should contain everything it needs.

You are correct. Run the executable.

When you create a self-contained app, the publish output "contains the complete set of files (both your app files and all .NET Core files) needed to launch your app." That includes the executable.

Example Self-Contained Deployment

Here is the output of dotnet publish -c release -r ubuntu.14.04-x64 for a simple self-contained application. Copy the publish directory to Ubuntu and run the executable.

C:\MyApp\bin\release\netcoreapp1.0\ubuntu.14.04-x64\publish\

...

libsos.so
libsosplugin.so
libuv.so
Microsoft.CodeAnalysis.CSharp.dll
Microsoft.CodeAnalysis.dll
Microsoft.CodeAnalysis.VisualBasic.dll
Microsoft.CSharp.dll
Microsoft.VisualBasic.dll
Microsoft.Win32.Primitives.dll
Microsoft.Win32.Registry.dll
mscorlib.dll
mscorlib.ni.dll
MyApp                        <------- On Ubuntu, run this executable
MyApp.deps.json                       and you will see Hello World!
MyApp.dll
MyApp.pdb
MyApp.runtimeconfig.json
sosdocsunix.txt
System.AppContext.dll
System.Buffers.dll
System.Collections.Concurrent.dll
System.Collections.dll

...

C:\MyApp\project.json

{
  "buildOptions": {
    "debugType": "portable",
    "emitEntryPoint": true
  },
  "dependencies": {},
  "frameworks": {
    "netcoreapp1.0": {
      "dependencies": {
        "Microsoft.NETCore.App": "1.0.1"
      }
    }
  },
  "runtimes": {
    "ubuntu.14.04-x64" : {},
    "win10-x64" : {}
  }
}

C:\MyApp\Program.cs

public class Program
{
    public static void Main(string[] args)
    {
        System.Console.WriteLine("Hello World!");
    }
}

See Also

This document differentiates between framework-dependent and self-contained deployments.



回答2:

Its worth noting that with the .netstandard2+, there are two required steps:

  • Edit the .csproj file and add a line with a list of target runtimes:

<PropertyGroup>

<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>

<!-- Add this with the required runtimes -->
<RuntimeIdentifiers>win10-x64;osx.10.11-x64;ubuntu.16.10-x64</RuntimeIdentifiers>

</PropertyGroup>

  • Restore and build the app: dotnet restore && dotnet build -c release -r RUNTIME

Where RUNTIME is one of the runtimes listed in the csproj file.

Importantly note that you cannot do this without the .csproj file edit and calling dotnet restore, or the runtime will not be downloaded from nuget, and the -r ... flag will not work.



回答3:

You may want to check out dotnet-packaging as well. It includes a dotnet deb command line utility which allows you to create a .deb file (i.e. a Ubuntu installer) which you can use to install your app on Ubuntu. It should make deployment easier for you.

To get started, you'll first need to add this section to your .csproj file:

<ItemGroup>
  <PackageReference Include="Packaging.Targets" Version="0.1.45" />
  <DotNetCliToolReference Include="dotnet-deb" Version="0.1.45" />
<ItemGroup>

Then, run dotnet restore and dotnet deb -c Release -r ubuntu.18.04-x64 -f netcoreapp2.0. This will create a .deb file you can use to deploy your application to Ubuntu.