【问题标题】:Cannot execute .NET application on Linux using 'dotnet'无法使用“dotnet”在 Linux 上执行 .NET 应用程序
【发布时间】:2018-04-19 19:07:57
【问题描述】:

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

前言

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

dotnet new console -o hwapp

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

cd hwapp
dotnet run

这已经打印了“Hello, World!”,这很好,因为一切都很好。

问题

我使用 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 或 Targeting Pack,或者将您的应用程序重新定位到已安装 SDK 或 Targeting Pack 的框架版本。请注意,程序集将从全局程序集缓存 (GAC) 中解析,并将用于代替引用程序集。因此,您的程序集可能无法正确定位于您想要的框架。 [/home/user/console/ConsoleApp1.csproj] 构建失败。请修复构建错误并重新运行。

我在网上搜索了这个错误,发现安装的.NET框架与应用程序版本不兼容,所以我尝试将应用程序编译为“4.5”等低版本的.NET框架,但是我遇到了同样的错误。

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

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

Visual Studio 控制台 (.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>bin\Debug\</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>bin\Release\</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="Properties\AssemblyInfo.cs" />
  </ItemGroup>
  <ItemGroup>
    <None Include="App.config" />
  </ItemGroup>
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

我该如何解决这个问题?

【问题讨论】:

  • 有效的是.net core,它可以跨平台工作。您在 Visual Studio 中创建的目标是完整的 .net 4.6.1 框架,它不是跨平台的
  • @mituw16 我已经想象过了。所以我想我无法使用dotnetLinux 上的其他实用程序运行使用Visual Studio 开发的应用程序,对吧?基本上我一直在寻找一种方法,可以让我每小时运行一个控制台应用程序,使用 cron 或类似的东西,所以我考虑过 Linux,但在这一点上我认为只有使用 IIS 才有可​​能,对吧?
  • 不,您可以在 Linux 上运行使用 Visual Studio 创建的应用程序,您只需要确保在创建项目时使用正确的框架,在本例中为 .net 核心
  • @mituw16 +1 给你,没注意到我必须在我的VS 上安装SDK,我试试,谢谢:)
  • 没问题! :)

标签: .net visual-studio .net-core


【解决方案1】:

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

Tutorial: Create a .NET Core console application using Visual Studio

【讨论】:

    猜你喜欢
    • 2021-07-19
    • 1970-01-01
    • 1970-01-01
    • 2019-10-04
    • 2022-10-04
    • 2018-09-03
    • 2018-03-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多