Why can't Microsoft analyzers find Microsoft.CodeAnalysis? - c#

I'm trying to add Microsoft.CodeAnalysis.FXCopAnalyzers (latest stable version) to my ASP.NET project. When I install it via NuGet, I get a ton of errors like:
An instance of analyzer Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.AbstractTypesShouldNotHaveConstructorsAnalyzer cannot be created from \packages\Microsoft.CodeQuality.Analyzers.2.6.3\analyzers\dotnet\cs\Microsoft.CodeQuality.Analyzers.dll : Could not load file or assembly 'Microsoft.CodeAnalysis, Version=2.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified..
An instance of analyzer Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.AvoidEmptyInterfacesAnalyzer cannot be created from \packages\Microsoft.CodeQuality.Analyzers.2.6.3\analyzers\dotnet\cs\Microsoft.CodeQuality.Analyzers.dll : Could not load file or assembly 'Microsoft.CodeAnalysis, Version=2.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified..
An instance of analyzer Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.CancellationTokenParametersMustComeLastAnalyzer cannot be created from \packages\Microsoft.CodeQuality.Analyzers.2.6.3\analyzers\dotnet\cs\Microsoft.CodeQuality.Analyzers.dll : Could not load file or assembly 'Microsoft.CodeAnalysis, Version=2.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified..
Note: For the sake of conciseness, I've omitted 127 of the errors. They are all very similar. One thing to note, though, is that not all of the analyzers are in the Microsoft.CodeQuality namespace; some are in Microsoft.NetCore, and some are in Microsoft.NetFramework.
So I say ok, I must need to install Microsoft.CodeAnalysis v2.6.0.0 first. Seems weird that that wouldn't be a dependency that's automatically installed by NuGet but whatever.
So I revert all my changes in Git, reopen the solution, install Microsoft.CodeAnalysis v2.6.0.0, and then install the latest stable version of Microsoft.CodeAnalysis.FXCopAnalyzers.
Same errors!
When I look in packages/Microsoft.CodeAnalysis.2.6.0, I notice that there isn't a .dll anywhere in there. What is the correct way to install this? Is installing Microsoft.CodeAnalysis even the correct solution? Why aren't the dependencies of Microsoft.CodeAnalysis.FXCopAnalyzers automatically installed when you install it? Isn't that the whole point of a package manager? I have so many questions and so few answers. Please help.

This issue on the Roslyn analyzers github project suggested adding a reference to Microsoft.Net.Compilers v2.6.1. I can't find this package anywhere in the dependency chain for Microsoft.CodeAnalysis.FXCopAnalyzers, but I noticed that I had version 1.0.0 installed in my project. I removed the package and installed v2.10.0, and now everything seems to be working as expected.

For anyone running into this problem in the future just to point out that one of the reasons this may happen is because VS is outdated.
A new installation or update of VS may solve the problem if the solution above doesn't.

Add on the *.csproj
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<AnalysisMode>AllEnabledByDefault</AnalysisMode>
<AnalysisLevel>latest</AnalysisLevel>
</PropertyGroup>

Related

Multi-targetted Library (monoandroid, xamarinios) issue with System.ComponentModel.DataAnnotations

I'm converting a (working) .netstandard library project into a multi-targetted project that will target:
<targetframeworks>netstandard2.1;xamarinios10;monoandroid10.0;monoandroid12.0;</targetframeworks>
I'm having issues with the System.ComponentModel.DataAnnotations reference. Sepcifically I get this error (for eveything except the .netstandard target):
The type 'DataType' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.ComponentModel.DataAnnotations, Version=2.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
After a bit of searching I tried adding the System.ComponentModel.Annotations (4.7.0) nuget package. This gave the same error.
In the nuget directory for this I noticed that where the .dll would normally live it had a file '.' - I didn't know if that was important (.nuget\packages\system.componentmodel.annotations\4.7.0\ref\MonoAndroid10) - the .netstandard dll was there as expected.
I've also noticed that (a separate) Xamarin.Android project seems to use the System.ComponentModel.DataAnnotations.dll which lives in C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Common7\IDE\ReferenceAssemblies\Microsoft\Framework\Xamarin.iOS\v1.0
I'm not sure if the right version of Mono.Android (or ios equivalant) is being targetted or I have to specify a specific assembly version, or whether I'm down the wrong track completely?
Thanks in advance,
Paul.
As the comment above mentions, to workaround this issue, I moved the bit of offending code (that used the System.ComponentModel.DataAnnotations) into a seperate project that targetted .netstandard2.1 and then referenced this project from my multi-targetted project.

Error: "Could not load file or assembly Newtonsoft.Json" appears during runtime after merging two branches. Both branches run fine on their own

I've been stuck with this bug after merging two branches together. The solution builds with no issues, but as soon as I call any function that uses Newtonsoft.Json I get the following exception at runtime:
Could not load file or assembly 'Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference.
I've looked around and have tried:
Re-installing the nuget package on the affected project
Re-installing the nuget package on all projects running Newtonsoft.Json
Updating all projects in the solution to be the same version of Newtonsoft.Json, then cleaning and
rebuilding the solution
Digging through .csproj files to find Newtonsoft references and manually updating them to the same
version.
When I manually copy and paste the 6.0.0.0 dll into the debug folder I get no issues, however as soon as I clean/ rebuild Visual Studio puts the wrong dll version into the folder again. Even after updating the Nuget package in the affected project to the latest version of Newtonsoft.Json the error still states that it is looking for version 6.0.0.0 rather than 12.0.0.0.
Am I missing something? Any help would be appreciated.
EDIT:
Using .net Framework 4.7
Just to clarify: when I say "wrong dll" I meant it puts the dll that I want it to use in the bin folder (12.0.0.0), but the program see's it as the wrong version, expecting version 6.0.0.0.
Was finally able to get it working. Ditched 12.0.0.0 completely, does not seem that there is a way to get Microsoft.AspNet.WebApi.Client working with any version beyond 6.0.0.0

Replace NuGet package with a local dll file

A project I am working on depends on a NuGet package. The dll file inside package is compiled in Release mode, therefore using debugger to inspect code (which is my goal) is impossible.
I'd like to replace the .dll file with the one I compiled in Debug mode. If I remove the package via NuGet and then add it via Add Reference, I get error:
Assembly 'AssemblyName' with identity 'AssemblyName, Version=4.2.5.0, Culture=neutral, PublicKeyToken=null' uses 'System.Memory, Version=4.0.1.1, Culture=neutral, PublicKeyToken=cf7b13ffcb2dde54'
which has a higher version than referenced assembly 'System.Memory' with identity 'System.Memory, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cf7b13ffcb2dde54'
What is the correct way to replace a NuGet package .dll with another .dll file? Where can the version number be changed I am using Rider, but learning how to do it in other IDEs is also useful.
This happens because the nuget is using a specific version of "System.Memory, Version=4.0.1.1", and you have System.Memory, Version=4.0.1.0 referenced in your project. You should update that reference and it should be good. So the problem is with your DLL not the NuGet. Probably when you removed the nuget you downgraded System.Memory, you should update that explicitly to the version that the error says it requires through NuGet.
Pro Tip if you want to mess around with nugets:
It is essentially a ZIP file
Download Nuget
Change extension to .zip
Decompress
Voila, you have some dll-s
You could ask the author of the nuget package to publish source and debug symbols of the package to a public symbolsource server and reference it. After that you would keep using the release version of the dll but be able to debug into it.

Could not load file or assembly 'Microsoft.Azure.CosmosDB.BulkImport, Version=2.0.0.0

At the runtime, I keep running into this issue:
Could not load file or assembly 'Microsoft.Azure.CosmosDB.BulkImport, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies.
I believe BulkImport comes from the BulkExecutor package, and I can find BulkExecutor package in packages.config:
<package id="Microsoft.Azure.CosmosDB.BulkExecutor" version="1.1.0" targetFramework="net462" />
But I cannot find the Microsoft.Azure.CosmosDB.BulkImport mentioned in neither packages.config nor app.config.
I can, however, find Microsoft.Azure.CosmosDB.BulkImport.dll in
\src\packages\Microsoft.Azure.CosmosDB.BulkExecutor.1.1.0\lib\net451\
and this is referenced in .csproj file.
I tried clearing the bin folder, clean/build project, uninstalling and reinstalling BulkExecutor library, rebooting Visual Studio 2015, but did not work.
I'm using VS 2015, Microsoft.Azure.CosmosDB.BulkExecutor v.1.1.0, .NET Framework v.4.6.2, Microsoft.Azure.DocumentDB v.2.0.0 and Newtonsoft.Json v.10.0.2.
But I cannot find the Microsoft.Azure.CosmosDB.BulkImport mentioned in
neither packages.config nor app.config.
Download the nuget package, rename it to microsoft.azure.cosmosdb.bulkexecutor.1.1.0.zip then you can check the content.
You'll find the Microsoft.Azure.CosmosDB.BulkImport.dll is part of the Microsoft.Azure.CosmosDB.BulkExecutor nuget package. When you reference the Microsoft.Azure.CosmosDB.BulkExecutor package in packages.config file, you're actually referencing the Microsoft.Azure.CosmosDB.BulkImport.dll.
And as I know, the normal Hintpath for packages.config format in xx.csproj should be something like:
<HintPath>..\packages\Microsoft.Azure.CosmosDB.BulkExecutor.1.1.0\lib\net451\Microsoft.Azure.CosmosDB.BulkImport.dll</HintPath>
At the runtime, I keep running into this issue:
So the compile and build succeeded, this issue only occur in runtime? If so, please check whether the Microsoft.Azure.CosmosDB.BulkImport.dll is in your output folder.(Usually bin folder).
For your error message, maybe your assembly can't find the Microsoft.Azure.CosmosDB.BulkImport.dll it referenced if the assembly wasn't copied to output folder.You can click the Microsoft.Azure.CosmosDB.BulkImport.dll in Solution Explorer, then set Copy Local to true in Properties Window.
If the issue persists though the Microsoft.Azure.CosmosDB.BulkImport.dll and your assembly or executable are in same folder, maybe you have a similar situation like this issue. The issue could result from your code, it'll be better if you could share some simple code in your question to reproduce it.

Type not found Microsoft.EntityFrameworkCore in .NET Standard project

I am having a total nightmare getting Entity Framework Core working in my .NET standard project! I am using .NET Standard 2
I can install the package fine (Microsoft.EntityFrameworkCore.SqlServer)
However, when I build my application although it compiles, I get the error below
Could not load file or assembly 'Microsoft.EntityFrameworkCore, Version=2.0.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' or one of its dependencies. The system cannot find the file specified.
Has anyone ever had this? I wasnt expecting this to be such a pain just to use Entity Framework in .NET Standard lol
I cant see any dlls related to this in my output folders
My application is hosted within service fabric, but I am not sure thats anything to do with it
Paul
I am assuming there is a bug either with Nuget or Visual Studio
I ended up having to manually copy all the dlls!!
Try to add this into your .csproj file which contains reference to the
Microsoft.EntityFrameworkCore.SqlServer
<PropertyGroup>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
</PropertyGroup>

Categories