关于c#:在.net核心控制台应用程序中使用.net类库时,项目构建失败

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

我有.Net Core 3.1控制台应用程序,它引用了作为参考添加的类库项目FxCore
类库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
Studio\\2019\\Professional\\MSBuild\\Current\\Microsoft.Common.props"
cannot be imported again. It was already imported at"C:\\Program
Files\\dotnet\\sdk\\3.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\\3.1.201\\Sdks\\Microsoft.NET.Sdk\\Sdk\\Sdk.targets(37,3):
warning MSB4011:"C:\\Program Files (x86)\\Microsoft Visual
Studio\\2019\\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
Studio\\2019\\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
Studio\\2019\\Professional\\MSBuild\\Current\\Microsoft.Common.props"
cannot be imported again. It was already imported at"C:\\Program
Files\\dotnet\\sdk\\3.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\\3.1.201\\Sdks\\Microsoft.NET.Sdk\\Sdk\\Sdk.targets(37,3):
warning MSB4011:"C:\\Program Files (x86)\\Microsoft Visual
Studio\\2019\\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
Studio\\2019\\Professional\\MSBuild\\Current\\Microsoft.Common.props"
cannot be imported again. It was already imported at"C:\\Program
Files\\dotnet\\sdk\\3.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\\3.1.201\\Sdks\\Microsoft.NET.Sdk\\Sdk\\Sdk.targets(37,3):
warning MSB4011:"C:\\Program Files (x86)\\Microsoft Visual
Studio\\2019\\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
Studio\\2019\\Professional\\MSBuild\\Current\\Microsoft.Common.props"
cannot be imported again. It was already imported at"C:\\Program
Files\\dotnet\\sdk\\3.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\\3.1.201\\Sdks\\Microsoft.NET.Sdk\\Sdk\\Sdk.targets(37,3):
warning MSB4011:"C:\\Program Files (x86)\\Microsoft Visual
Studio\\2019\\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
Studio\\2019\\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不会显示任何错误。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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将需要定位/多定位.NET Standard或.NET Core之一。

在选择目标/多目标方面:

  • 目标netstandard*-如果在实现中不需要任何特定于框架的内容
  • 多目标netcoreapp*netstandard*-如果要在可用的情况下使用netcoreapp*功能,或者使用下层通用实现,否则
  • 多目标netcoreapp*net###-如果实现完全是特定于平台的,并且不可能实现通用实现
  • (其中* / ###是您选择的版本-当前可能是netstandard2.1netcoreapp3.1net472 / net48)


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

    1
    2
    3
    4
      <PropertyGroup>
        <TargetFrameworks>net45;net451;net452;net46;net461;net462;net47;net471;net472</TargetFrameworks>
      </PropertyGroup>
      <PropertyGroup>