在Ubuntu上运行控制台应用程序时出现.NET Core 2.0错误。

6

我正在尝试在Ubuntu 16.04-x64上运行我的第一个.NET Core 2.0控制台应用程序。我按照发布Ubuntu应用程序的步骤进行操作:

dotnet publish -c release -r ubuntu.16.04-x64

我也尝试了从Visual Studio中进行尝试,方法是更改我的.csproj文件如下:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.0</TargetFramework>
    <RuntimeIdentifiers>ubuntu.16.04-x64</RuntimeIdentifiers>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="sharpadbclient" Version="2.1.0" />
    <PackageReference Include="System.IO.Ports" Version="4.4.0" />
  </ItemGroup>

</Project>

然后使用发布配置文件将其发布。 我按照Microsoft的指导在Ubuntu上安装了.NET Core,将已发布的输出复制到运行Ubuntu的PC上,并尝试运行我的控制台应用程序的.dll文件时,出现了以下错误:
Unhandled Exception: System.IO.FileLoadException: 
Could not load file or assembly
'System.Console, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
The located assembly's manifest definition does not match the assembly reference.
(Exception from HRESULT: 0x80131040)
   at LinuxVersion.Program.InitializeComponent()
   at LinuxVersion.Program.Main(String[] args)
Aborted (core dumped) 

当我运行dotnet restore时,我收到一条消息,上面写着:
MSBUILD : error MSB1003: Specify a project or solution file.
The current working directory does not contain a project or solution file.

我在这个过程中是不是漏掉了某一步骤?

你正在哪里运行dotnet restore命令? - Ibro
在运行Ubuntu的目标PC上 - Liran Friedman
2
你是使用 ./yourappname 运行已发布的应用程序还是 dotnet yourappname.dll?由于这是一个自包含的部署,很可能只有第一个选项可行 - 即使如此,你也应该使用 linux-x64 代替 ubuntu* 来运行 .NET Core 2.0。 - Martin Ullrich
我实际上是在使用第二种方法 dotnet myappname.dll,但我会尝试你建议的第一种方法。你认为这可能是导致这个特定错误的原因吗?另外,你所说的“使用linux-x64而不是ubuntu *”是什么意思?你是指在dotnet publish命令中吗? - Liran Friedman
{btsdaf} - Harit Kumar
1个回答

7
好的,原来使用Visual Studio发布配置文件(右键单击项目并选择“发布”)和使用命令行发布应用程序之间存在差异。当我使用Visual Studio发布配置文件时,我遇到了这个错误,然后我切换到使用命令行,像这样:dotnet publish -c release -r ubuntu.16.04-x64,但要运行它,我进入输出的发布文件夹:cd /home/MyApp/publish,然后使用dotnet MyAppName.dll运行应用程序。

这对我解决了问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接