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?

Daniel
  • 8,655
  • 5
  • 60
  • 87

3 Answers3

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

Daniel
  • 8,655
  • 5
  • 60
  • 87
  • Thanks Elendil, do you have an answer to my second question? Would the proper solution be to make a multi-targeted library? – Daniel Feb 05 '20 at 15:24
  • Hi @Daniel , 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 https://learn.microsoft.com/en-us/aspnet/core/migration/22-to-30?view=aspnetcore-3.1&tabs=visual-studio#analyzer-support, starting from 3.0, Mvc.Analyzers is implicitly referenced, so you can try add true in your csproj file as specified in the link – Elendil Zheng-MSFT Feb 06 '20 at 03:04
  • 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 https://learn.microsoft.com/en-us/dotnet/core/tutorials/libraries#how-to-multitarget – Elendil Zheng-MSFT Feb 06 '20 at 03:15
  • 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! – Daniel Feb 07 '20 at 04:50
  • The final solution I used was to make my project multi-target. – Daniel Feb 13 '20 at 20:51
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>
tkit
  • 8,082
  • 6
  • 40
  • 71
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.

Neil
  • 1,613
  • 1
  • 16
  • 18