在 .net 核心控制台应用程序中使用 .net class 库时项目构建失败

Project build failed while using .net class library in .net core console application

我有 .Net Core 3.1 控制台应用程序,它引用 class 库项目 FxCore 作为参考添加。 class 库 csproj 有 <TargetFrameworks>net45;net451;net452;net46;net461;net462;net47;net471;net472</TargetFrameworks> 我已经尝试将其更改为 <TargetFrameworks>net45;</TargetFrameworks>,但仍然出现相同的错误。

我收到以下错误。,我已尝试清理 -> 重建,重新打开 visual studio 2019(版本 16.5.2)

1>------ Build started: Project: FxCore, Configuration: Debug Any CPU ------ 1>J:\Test\core\fx-core\FxCore.csproj(3,3): warning MSB4011: "C:\Program Files (x86)\Microsoft Visual Studio19\Professional\MSBuild\Current\Microsoft.Common.props" cannot be imported again. It was already imported at "C:\Program Files\dotnet\sdk.1.201\Sdks\Microsoft.NET.Sdk\Sdk\Sdk.props (40,3)". This is most likely a build authoring error. This subsequent import will be ignored. 1>C:\Program Files\dotnet\sdk.1.201\Sdks\Microsoft.NET.Sdk\Sdk\Sdk.targets(37,3): warning MSB4011: "C:\Program Files (x86)\Microsoft Visual Studio19\Professional\MSBuild\Current\Bin\Microsoft.CSharp.targets" cannot be imported again. It was already imported at "J:\Test\core\fx-core\FxCore.csproj (120,3)". This is most likely a build authoring error. This subsequent import will be ignored. 1>C:\Program Files (x86)\Microsoft Visual Studio19\Professional\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2081,5): warning MSB3245: Could not resolve this reference. Could not locate the assembly "FXEntity, Version=1.0.1.12, Culture=neutral, processorArchitecture=MSIL". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors. 1>FxCore -> J:\Test\core\fx-core\bin\Debug\FxCore.dll 1>J:\Test\core\fx-core\FxCore.csproj(3,3): warning MSB4011: "C:\Program Files (x86)\Microsoft Visual Studio19\Professional\MSBuild\Current\Microsoft.Common.props" cannot be imported again. It was already imported at "C:\Program Files\dotnet\sdk.1.201\Sdks\Microsoft.NET.Sdk\Sdk\Sdk.props (40,3)". This is most likely a build authoring error. This subsequent import will be ignored. 1>C:\Program Files\dotnet\sdk.1.201\Sdks\Microsoft.NET.Sdk\Sdk\Sdk.targets(37,3): warning MSB4011: "C:\Program Files (x86)\Microsoft Visual Studio19\Professional\MSBuild\Current\Bin\Microsoft.CSharp.targets" cannot be imported again. It was already imported at "J:\Test\core\fx-core\FxCore.csproj (120,3)". This is most likely a build authoring error. This subsequent import will be ignored. 1>J:\Test\core\fx-core\FxCore.csproj(3,3): warning MSB4011: "C:\Program Files (x86)\Microsoft Visual Studio19\Professional\MSBuild\Current\Microsoft.Common.props" cannot be imported again. It was already imported at "C:\Program Files\dotnet\sdk.1.201\Sdks\Microsoft.NET.Sdk\Sdk\Sdk.props (40,3)". This is most likely a build authoring error. This subsequent import will be ignored. 1>C:\Program Files\dotnet\sdk.1.201\Sdks\Microsoft.NET.Sdk\Sdk\Sdk.targets(37,3): warning MSB4011: "C:\Program Files (x86)\Microsoft Visual Studio19\Professional\MSBuild\Current\Bin\Microsoft.CSharp.targets" cannot be imported again. It was already imported at "J:\Test\core\fx-core\FxCore.csproj (120,3)". This is most likely a build authoring error. This subsequent import will be ignored. 1>J:\Test\core\fx-core\FxCore.csproj : warning NU1603: FxCore depends on Microsoft.Practices.ServiceLocation (>= 1.3.0) but Microsoft.Practices.ServiceLocation 1.3.0 was not found. An approximate best match of Microsoft.Practices.ServiceLocation 1.4.11 was resolved. 1>Done building project "FxCore.csproj". 1>Done building project "FxCore.csproj". 2>------ Build started: Project: CoreConsoleApp, Configuration: Debug Any CPU ------ 2>J:\Test\core\fx-core\FxCore.csproj(3,3): warning MSB4011: "C:\Program Files (x86)\Microsoft Visual Studio19\Professional\MSBuild\Current\Microsoft.Common.props" cannot be imported again. It was already imported at "C:\Program Files\dotnet\sdk.1.201\Sdks\Microsoft.NET.Sdk\Sdk\Sdk.props (40,3)". This is most likely a build authoring error. This subsequent import will be ignored. 2>C:\Program Files\dotnet\sdk.1.201\Sdks\Microsoft.NET.Sdk\Sdk\Sdk.targets(37,3): warning MSB4011: "C:\Program Files (x86)\Microsoft Visual Studio19\Professional\MSBuild\Current\Bin\Microsoft.CSharp.targets" cannot be imported again. It was already imported at "J:\Test\core\fx-core\FxCore.csproj (120,3)". This is most likely a build authoring error. This subsequent import will be ignored. 2>C:\Program Files (x86)\Microsoft Visual Studio19\Professional\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(1655,5): warning NU1702: ProjectReference 'J:\Test\core\fx-core\FxCore.csproj' was resolved using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETCoreApp,Version=v3.1'. This project may not be fully compatible with your project. 2>J:\Test\core\CoreConsoleApp\Program.cs(2,7,2,9): error CS0246: The type or namespace name 'FX' could not be found (are you missing a using directive or an assembly reference?) 2>J:\Test\core\CoreConsoleApp\Program.cs(7,24,7,32): error CS0246: The type or namespace name 'FxCommon' could not be found (are you missing a using directive or an assembly reference?) 2>Done building project "CoreConsoleApp.csproj" -- FAILED. ========== Build: 1 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

但是在 Program.cs 我可以使用代码,IntelliSense 没有显示任何错误。

using System;
using FX.Core;
namespace CoreConsoleApp
{
    class Program
    {
        private static FxCommon _fxCommon = new FxCommon();
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            _fxCommon.Logger.LogWrite("hello");
        }
    }
}

因此我无法 运行 控制台应用程序,请帮忙。

.NET Core 不使用 .NET Framework (net###) 目标;它可以消耗 netstandard*netcoreapp*.

所以;您的 FxCore 将需要 target/multi-target .NET Standard 或 .NET Core 之一。

什么到target/multi-target的选择方面:

  1. 目标 netstandard* - 如果您在实现中不需要任何特定于框架的内容
  2. 多目标 netcoreapp*netstandard* - 如果您想在可用时使用 netcoreapp* 功能,否则使用下层通用实现
  3. 多目标 netcoreapp*net### - 如果实现完全是特定于平台的并且不可能有共同的实现

(其中 * / ### 是您选择的版本 - 可能是 netstandard2.1netcoreapp3.1net472/net48当前时间)

我已经通过从 class 库 csproj 文件中删除以下行解决了这个问题。

  <PropertyGroup>
    <TargetFrameworks>net45;net451;net452;net46;net461;net462;net47;net471;net472</TargetFrameworks>
  </PropertyGroup>
  <PropertyGroup>