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

别来无恙 提交于 2020-07-16 09:40:44

问题


I have .Net Core 3.1 console application which is referring to a class library project FxCore which is added as a reference. class library csproj has <TargetFrameworks>net45;net451;net452;net46;net461;net462;net47;net471;net472</TargetFrameworks> I have tried changing it to <TargetFrameworks>net45;</TargetFrameworks>, but still getting the same error.

I am getting the below error., I have tried clean -> ReBuild, Reopen the visual studio 2019 (version 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 ==========

But in the Program.cs I am able to use the code, IntelliSense not showing any error.

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");
        }
    }
}

I am not able to run the console application due to this, please help.


回答1:


.NET Core doesn't consume .NET Framework (net###) targets; it can consume netstandard* and netcoreapp*.

So; your FxCore is going to need to target/multi-target one of .NET Standard or .NET Core.

In terms of choice of what to target/multi-target:

  1. target netstandard* - if you don't need anything framework-specific in the implementation
  2. multi-target netcoreapp* and netstandard* - if you want to use netcoreapp* features when they are available, or use a down-level common implementation otherwise
  3. multi-target netcoreapp* and net### - if the implementations are completely platform specific and no common implementation is possible

(where * / ### is your choice of versions - probably netstandard2.1, netcoreapp3.1 and net472/net48 at the current time)




回答2:


I have solved the issue by removing the below lines from the class library csproj file.

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


来源:https://stackoverflow.com/questions/62789164/project-build-failed-while-using-net-class-library-in-net-core-console-applica

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!