[英]Running a self-contained ASP.NET Core application on Ubuntu
I've published an ASP.NET Core application as a self-contained application targeting Ubuntu.我发布了一个 ASP.NET Core 应用程序作为面向 Ubuntu 的自包含应用程序。 The publish seems to work fine.
发布似乎工作正常。 I've copied the files to a pretty vanilla Ubuntu machine.
我已经将这些文件复制到了一台非常普通的 Ubuntu 机器上。 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.我的理解是,因为它是一个自包含.NET核心应用我并不需要下载并安装.NET的核心东西。 My application should contain everything it needs.
我的应用程序应该包含它需要的一切。
All tutorials seem to say I should call dotnet run
.所有教程似乎都说我应该调用
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".但是,“dotnet”命令行不存在(它应该发布到自包含文件夹中吗??)所以如果我调用它,我会得到“找不到命令”。 Of course I could download .NET Core, but doesn't that go against the whole self-contained concept?
我当然可以下载 .NET Core,但这不违背整个独立的概念吗?
Here is a sample of the files I'm copying over:这是我正在复制的文件示例:
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.
我的理解是,因为它是一个自包含.NET核心应用我并不需要下载并安装.NET的核心东西。 My application 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."创建自包含应用程序时,发布输出“包含启动应用程序所需的完整文件集(应用程序文件和所有 .NET Core 文件)。” That includes the executable.
这包括可执行文件。
Here is the output of dotnet publish -c release -r ubuntu.14.04-x64
for a simple self-contained application.这是一个简单的自包含应用程序的
dotnet publish -c release -r ubuntu.14.04-x64
的输出。 Copy the publish directory to Ubuntu and run the executable.将发布目录复制到 Ubuntu 并运行可执行文件。
C:\\MyApp\\bin\\release\\netcoreapp1.0\\ubuntu.14.04-x64\\publish\\ 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 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 C:\\MyApp\\Program.cs
public class Program
{
public static void Main(string[] args)
{
System.Console.WriteLine("Hello World!");
}
}
This document differentiates between framework-dependent and self-contained deployments. 本文档区分依赖于框架的部署和独立的部署。
Follow the below steps to run your application:按照以下步骤运行您的应用程序:
Publish your application as a self contained application:将您的应用程序发布为自包含应用程序:
dotnet publish -c release -r ubuntu.16.04-x64 --self-contained
Copy the publish
folder to the Ubuntu machine将
publish
文件夹复制到 Ubuntu 机器
Open the Ubuntu machine terminal (CLI) and go to the project directory打开Ubuntu机器终端(CLI),进入项目目录
Provide execute permissions:提供执行权限:
chmod 777 ./appname
Execute the application执行应用程序
./appname
Author: Harit Kumar添加一名作者
Original answer here: How to run a .NET Core console application on Linux原始答案在这里: 如何在 Linux 上运行 .NET Core 控制台应用程序
It's worth noting that with the .NET Standard 2 +, there are two required steps:值得注意的是,在.NET Standard 2+ 中,有两个必需的步骤:
<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>
</PropertyGroup>
dotnet restore && dotnet build -c release -r RUNTIME
dotnet restore && dotnet build -c release -r RUNTIME
Where RUNTIME is one of the runtimes listed in the .csproj file.其中 RUNTIME 是 .csproj 文件中列出的运行时之一。
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.重要的是请注意,如果没有 .csproj 文件编辑和调用
dotnet restore
,则无法执行此操作,否则不会从NuGet下载运行时,并且-r ...
标志将不起作用。
You may want to check out dotnet-packaging as well.您可能还想查看dotnet-packaging 。 It includes a
dotnet deb
command line utility which allows you to create a .deb
file (ie a Ubuntu installer) which you can use to install your app on Ubuntu.它包括一个
dotnet deb
命令行实用程序,它允许您创建一个.deb
文件(即 Ubuntu 安装程序),您可以使用它在 Ubuntu 上安装您的应用程序。 It should make deployment easier for you.它应该使您的部署更容易。
To get started, you'll first need to add this section to your .csproj
file:首先,您首先需要将此部分添加到您的
.csproj
文件中:
<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
.然后,运行
dotnet restore
和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.这将创建一个
.deb
文件,可用于将应用程序部署到 Ubuntu。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.