Razor Class Library MSBuild MSB4062 Error During C

2019-08-27 04:18发布

问题:

I'm trying to figure out how to solve MSB4062 error when I'm building a MVC application that references a Razor class library. The steps to reproduce it involve creating a new MVC project (running "dotnet new mvc"), creating a new Razor Class Library (running "dotnet new razorclasslib"), and adding the class library to the MVC application by adding the following to the csproj file:

<ItemGroup>
    <ProjectReference Include="..\Library\Library.csproj" />
</ItemGroup>

As soon as I try to compile the project (I'm using Visual Studio Code), I get the following error message:

C:\Program Files\DotNET\sdk\NuGetFallbackFolder\microsoft.aspnetcore.razor.design\2.1.2\build\netstandard2.0\Microsoft.AspNetCore.Razor.Design.CodeGeneration.targets(69,5): error MSB4062: The "Microsoft.AspNetCore.Razor.Tasks.RazorTagHelper" task could not be loaded from the assembly C:\Program Files\DotNET\sdk\NuGetFallbackFolder\microsoft.aspnetcore.razor.design\2.1.2\build\netstandard2.0....\tasks\netstandard2.0\Microsoft.AspNetCore.Razor.Tasks.dll. Assembly with same name is already loaded Confirm that the declaration is correct, that the assembly and all its dependencies are available, and that the task contains a public class that implements Microsoft.Build.Framework.ITask. [C:\Users\userprofile\Development\Application\Application.csproj]

The really strange part is that if I then remove the ProjectReference entry in the csproj file, compile the MVC application (without the library), then re-add the ProjectReference entry, the whole project (with library) compiles fine. However, as soon as I clean either the MVC application or library the error comes back.

回答1:

Adding the following to the razor class library's csproj file fixed the issue:

<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.1.2" PrivateAssets="All" />

A similar reference already exists by default in the application's csproj file ... maybe there was a version mismatch?