简体   繁体   English

在 Ubuntu 上运行独立的 ASP.NET Core 应用程序

[英]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:这是我正在复制的文件示例:

在此处输入图片说明

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.我的理解是,因为它是一个自包含.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.这包括可执行文件。

Example Self-Contained Deployment独立部署示例

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!");
    }
}

See Also另见

This document differentiates between framework-dependent and self-contained deployments. 本文档区分依赖于框架的部署和独立的部署。

Follow the below steps to run your application:按照以下步骤运行您的应用程序:

  1. Publish your application as a self contained application:将您的应用程序发布为自包含应用程序:

     dotnet publish -c release -r ubuntu.16.04-x64 --self-contained
  2. Copy the publish folder to the Ubuntu machinepublish文件夹复制到 Ubuntu 机器

  3. Open the Ubuntu machine terminal (CLI) and go to the project directory打开Ubuntu机器终端(CLI),进入项目目录

  4. Provide execute permissions:提供执行权限:

     chmod 777 ./appname
  5. 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+ 中,有两个必需的步骤:

  • Edit the .csproj file and add a line with a list of target runtimes:编辑 .csproj 文件并添加一行包含目标运行时列表:

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

  • Restore and build the app: 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 restoredotnet 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.

相关问题 Asp.Net Core WebAPI [dotnet publish --self-contained -r linux-64] 放入Docker容器后真的是自包含吗 - Asp.Net Core WebAPI [dotnet publish --self-contained -r linux-64] is it really self-contained after putting into Docker container 发布用于Linux体系结构的.NET Core C#自包含应用程序 - Publishing .NET Core C# self-contained application for linux architecture Win 10 -&gt; VS 2019 -&gt; .NET Core 3 WPF 应用程序 -&gt; 发布到目录(对于 linux-x64 自包含) -&gt; 发布失败 - Win 10 -> VS 2019 -> .NET Core 3 WPF application -> Publish to directory (self-contained for linux-x64) -> Publish failed 部署自包含的本机OCaml应用程序 - Deploying self-contained native OCaml application 在 linux 上运行 .net 核心自包含可执行文件的 glibc 错误 - glibc error running a .net core self contained executable on linux 在Linux中创建一个自包含的python可执行文件 - Creating a self-contained python executable in Linux 如何使 Unix 二进制自包含? - How to make Unix binary self-contained? 使用Mono在Ubuntu中运行基于DevExpress的ASP.NET Web应用程序时出错 - Errors Running DevExpress based ASP.NET Web Application In Ubuntu With Mono 如何为 ARM 制作自包含或 Java 捆绑的 JavaFX 应用程序包 - How to make self-contained or java-bundled JavaFX application package for ARM ASP.NET Core 1.0.1不适用于Ubuntu / Linux吗? - ASP.NET Core 1.0.1 not avaliable for Ubuntu/Linux?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM