无法使用 'dotnet' 在 Linux 上执行 .NET 应用程序



我正在寻找一种在Linux上执行.NET应用程序的方法,我发现了dotnet

前言

我能够在 Ubuntu 17.10.1(Artful Aardvark)上配置dotnet,所以我尝试使用以下命令创建一个简单的 .NET 应用程序:

dotnet new console -o hwapp

这效果很好。该命令在hwapp文件夹中创建了一个简单的Hello, World!应用程序。为了进行测试,如果这有效,我执行:

cd hwapp
dotnet run

这已经打印了"你好,世界!">这很好,因为一切都很好。

问题所在

我使用 Visual Studio 2017 创建了一个控制台应用程序。此应用程序本质上与使用dotnet创建的相同,并包含一个简单的"Hello World"。

我把它传递给 Ubuntu,特别是在console文件夹中,然后我执行以下命令:

cd console
dotnet run

但是,不幸的是,我收到此警告消息:

/

usr/share/dotnet/sdk/2.0.0/Microsoft.Common.CurrentVersion.targets(1122,5):错误MSB3644:框架的引用程序集"。未找到 NetFramework,Version=v4.6.1"。若要解决此问题,请为此框架版本安装 SDK 或目标包,或将应用程序重定向到已安装 SDK 或目标包的框架版本。请注意,程序集将从全局程序集缓存 (GAC) 解析,并将用于代替引用程序集。因此,您的程序集可能不是您期望的框架的正确目标。[/home/user/console/ConsoleApp1.csproj] 生成失败。请修复构建错误并再次运行。

我在互联网上搜索了这个错误,我发现安装的.NET框架与应用程序版本不兼容,所以我试图将应用程序编译为.NET框架的较小版本,例如"4.5",但我得到了同样的错误。

我还将使用dotnet创建的控制台应用程序的 .csproj 文件与 Visual Studio 控制台应用程序进行了比较。第一个具有以下结构:

<Project Sdk="Microsoft.Net.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
</Project>

Visual Studio console (.csproj):

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)$(MSBuildToolsVersion)Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)$(MSBuildToolsVersion)Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{1E8D1AE9-C1A2-48D5-B183-3D958885A3BB}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>ConsoleApp1</RootNamespace>
<AssemblyName>ConsoleApp1</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>binDebug</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>binRelease</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="PropertiesAssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)Microsoft.CSharp.targets" />
</Project>

我该如何解决这个问题?

您必须在Visual Studio项目选择中选择".NET Core",而不是旧技术的.NET Framework。

教程:使用 Visual Studio 创建 .NET Core 控制台应用程序

最新更新