Building ASP.NET-Core 3.1 with .NET-Standard 2.0 projects leads to conflicting Microsoft.AspNetCore.Mvc.Analyzers assemblies
Asked Answered
T

3

11

I'm trying to build an ASP.NET-Core 3.1 (netcoreapp3.1) application which has a dependency on a NuGet library that is .NET-Standard 2.0 which uses MSBuild SDK "Microsoft.NET.Sdk.Razor".

This builds and runs fine from Visual Studio (2019) but when I run dotnet build I get the following error:

Build FAILED.

CSC : error CS8034: Unable to load Analyzer assembly 
 C:\Users\daniel\.nuget\packages\microsoft.aspnetcore.mvc.analyzers\2.2.0\analyzers\dotnet\cs\Microsoft.AspNetCore.Mvc.Analyzers.dll
 : Assembly with same name is already loaded [D:\git\myapp\src\myapp.App\myapp.App.csproj]
    0 Warning(s)
    1 Error(s)

My guess is that my .NET-Standard 2.0 library is pulling in Microsoft.CodeQuality.Analyzers 2.x via the Microsoft.NET.Sdk.Razor SDK and this conflicts with the one being pulled in by the ASP.NET-Core 3.1 application.

Questions:

  1. Is there either a way to build my application via command line in the same way Visual Studio does it?

  2. Is the proper solution to use multi-targeting and #if NETCOREAPP3_1 blocks in my library?

Toughen answered 5/2, 2020 at 2:39 Comment(1)
I am still getting this too, even after adding <DisableImplicitComponentsAnalyzers>true</DisableImplicitComponentsAnalyzers> to the project file.Rainbolt
C
3

visual studio uses MSBuild to build your solution file, so you can try MSBuild %yoursolutionfile% to build your solution, on the other hand, dotnet build typically builds a single project instead of solution.

to answer your second question, if you do find that your dependency is required and can not be ignored in dotnet build for 3.1 version, yes a multitarget setup should work, you can follow this link to build a multitarget application https://learn.microsoft.com/en-us/dotnet/core/tutorials/libraries#how-to-multitarget

Clark answered 5/2, 2020 at 3:51 Comment(5)
Thanks Elendil, do you have an answer to my second question? Would the proper solution be to make a multi-targeted library?Toughen
Hi @Toughen , I can see in your error message it is saying Unable to load Analyzer assembly C:\Users\daniel\.nuget\packages\microsoft.aspnetcore.mvc.analyzers\2.2.0\analyzers\dotnet\cs\Microsoft.AspNetCore.Mvc.Analyzers.dll. you can see in this link learn.microsoft.com/en-us/aspnet/core/migration/…, starting from 3.0, Mvc.Analyzers is implicitly referenced, so you can try add <IncludeOpenAPIAnalyzers>true</IncludeOpenAPIAnalyzers> in your csproj file as specified in the linkClark
back to your second question, if you do find that your dependency is required and can not be ignored in dotnet build for 3.1 version, yes a multitarget complie should work, you can follow this link to build a multitarget application learn.microsoft.com/en-us/dotnet/core/tutorials/…Clark
Thanks for the replies Elendil. I did see that note in the migration guide but I never explicitly added API analysers in my netstandard20 project so I didn't think I needed to do this. Also I think adding another set of analysers would cause more conflicts but we'll see! I did make a multi-target library and that works great!Toughen
The final solution I used was to make my project multi-target.Toughen
A
3

This is a workaround to hide the annoying message, but it will not actually remove the underlying issue:

Edit the .csproj and add a 8034 (CS8034) <NoWarn> to the Configuration/Platform <PropertyGroup> like this:

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
    <!-- ... -->
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <NoWarn>8034</NoWarn>
  </PropertyGroup>

  <!-- ... -->
</Project>
Arsine answered 5/1, 2021 at 21:48 Comment(0)
A
0

Semi-related, but this question is one of the top search results for error CS8034 so I want to leave an answer for others. In my case we ran into the error with a different analyzer:

CS8034: Unable to load Analyzer assembly C:\...\.nuget\packages\strawberryshake.codegeneration.csharp.analyzers\12.0.1\analyzers\dotnet\cs\Microsoft.CodeAnalysis.CSharp.dll

We had StrawberryShake installed on a .NetStandard2.0 project, being built by .Net 3.1 sdk. However, StrawberryShake uses Roslyn Source Generators which were released with .Net 5. Downstream the csprojs that depend on our StrawberryShake code failed to compile, saying the assembly didn't have any of our generated namespaces / types. We upgraded to .Net 5 for building the StrawberryShake project and our issues went away.

Antichlor answered 19/11, 2021 at 15:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.