Solution has compiled successfully, but after I added an existing class file to the project, this error appeared:
The specified task executable "csc.exe" could not be run. Could not
load file or assembly 'System.Security.Principal.Windows,
Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or
one of its dependencies. The system cannot find the file
specified. MvcApplicationRegister
I installed System.Security.Principal.Windows package By NuGet, but error still appears.
None of the previous answers worked for me.
The problem was that I didn't have the .NET Compiler Platform SDK installed.
To solve, open Visual Studio Installer, choose "Modify", and under the "Invididual Component" tab, check the .NET Compiler Platform SDK, and confirm your changes by clicking "Modify".
After I installed it and reopened Visual Studio, the problem is gone.
I had the same issue after I upgraded Microsoft.Net.Compiler from 2.8.2 to 2.9.0.
After I downgraded to 2.8.2 projects compiled without any errors.
Had this same issue and resolved it.
In a 3 project solution MVC controller (Web,Business,Data)
Caused by the Microsoft.Net.Compiler 2.9.0 being installed on the Web project but not the other projects.
To resolve:
Right click the solution.
Manage NuGet Packages.
Installed > Search for the compiler
Ensure it is the same version and it is installed on all projects in your solution
Once installed my solution built successfully
If you are using Microsoft.CodeDom.Providers.DotNetCompilerPlatform you can upgrade to 2.x and then remove Microsoft.Net.Compilers as it's no longer needed. That solved it for me, however I couldn't even build the solution in the first place. It was still complaining about System.Security.Principal.Windows though, I could also solve it by referencing System.Security as an assembly. It's not recommended though.
Close down all instances of Visual Studio. Then reopen the solution and rebuild.
I uninstalled both Microsoft.CodeDom.Providers.DotNetCompilerPlatform and Microsoft.Net.Compilers and everything now works.
In my case I could go from Microsoft.Net.Compilers 2.4.0 to Microsoft.Net.Compilers 2.10.0. No need to use Microsoft.Net.Compilers 2.8.2.
I do not like removing packages without first understanding what I'm removing. I faced the same problem with my solution.
I discovered that 1 of the many projects was using the Microsoft.Net.Compilers NuGet package - let's call it Project ABC. A Unit Test project was referencing that Project ABC, but not the Microsoft.Net.Compilers NuGet package.
I simply referenced the Microsoft.Net.Compilers NuGet package from my Unit Test project, and the problem has now gone away.
Updating Microsoft.CodeDom.Providers.DotNetCompilerPlatform and deleting Microsoft.Net.Compilers worked for me.
I had the same issue here using 'System.Security.Principal.Windows' version 4.7.0. For some reason when I installed the nuget package it was referecing the dll from the folder: C:\Users\{your-user}\.nuget\packages\system.security.principal.windows\4.7.0\ref\netstandard2.0\System.Security.Principal.Windows.dll then I changed to reference the dll from the folder C:\Users\{your-user}\.nuget\packages\system.security.principal.windows\4.7.0\lib\netstandard2.0\System.Security.Principal.Windows.dll and everything worked just fine! So I copied the dll from the lib folder to my project and made a direct reference.
Related
After migrating my project from .NET Core 2.1 to .NET Core 3.1, everything works fine but when publishing the project, Iget this error:
System.Runtime.CompilerServices.Unsafe doesn't support netcoreapp3.1. Consider updating your TargetFramework to netcoreapp3.1 or later.
I resolved the problem after remove the tag <RuntimeIdentifier> from the publish profile.
Try this first!
For anyone suddenly running into this error for seemingly no reason, the error message may be a red herring. You should try deleting all bin and obj folders first before anything else.
For each project in your solution, locate and delete the obj and bin folders (don't worry, they will be regenerated automatically).
Right-click the solution and click "Clean Solution"
Right-click your startup project and click "Rebuild"
For me, the problem occurred while attempting to publish, even though (a) no changes had taken place, (b) it worked the day before, and (c) I was actually using .NET 6.0!
After failing to fix the issue through any recommended solutions, I went ahead and actually removed the warning from the .targets file in the System.Runtime.CompilerServices.Unsafe package folder (i.e., I suppressed the error), but then the next package in the list started complaining about the same thing.
Solution
Deleting the bin/obj folders and cleaning the solution fixed the issue immediately.
The root cause of the issue for me is, one of the NuGet packages is referring to "Microsoft.Extensions.DependencyModel" v6.0.0. After downgrading the "Microsoft.Extensions.DependencyModel" NuGet to version 5.0.0 issue is resolved.
Please refer to the link for more info,
https://github.com/dotnet/runtime/issues/62561
##Morteza install System.Runtime.CompilerServices.Unsafe version 6.0.0 from NuGet into your project.
Install-Package System.Runtime.CompilerServices.Unsafe -Version 6.0.0
In Visual Studio with my Asp.net core project, I have used some nuget packets (like StackExchange.Redis, Serilog etc..). I decided to use Visual studio 2019. Everything was going well, but one day I noticed some projects couldn't update their referenced projects in solution. Because they are (.net standard libraries) taking output codes to obj folder instead bin folder.
I decided to return Visual Studio 2017. Everything working great with VS2017, but this time it giving me a run time error such as could not found referenced assembly public key etc.. like below
I have removed old pockets from all projects in the solution by the package manager nuget manager. I have installed a new version by the nuget manager tool. But project always seemto be using the old version.
FileNotFoundException: Could not load file or assembly 'StackExchange.Redis, Version=2.0.0.0, Culture=neutral, PublicKeyToken=c219ff1ca8c2ce46'. The system cannot find the file specified.
For example, if I install a nuget packet version 2.0.0.0 and I want to remove it all from project and I want to install a new version by an upgrade or downgrade. What should I do? How can I change it?
Update: I have uninstalled 2 packages from nuget and from windows, still project seen this assemblies!
Severity Code Description Project File Line Suppression State
Error CS0433 The type 'ConnectionMultiplexer' exists in both
'StackExchange.Redis.StrongName, Version=1.2.6.0, Culture=neutral,
PublicKeyToken=c219ff1ca8c2ce46' and 'StackExchange.Redis,
Version=2.0.0.0, Culture=neutral,
PublicKeyToken=c219ff1ca8c2ce46' Tursys.Pool.Storage.Api D:\projects\tursys\PoolStorage\src\Tursys.Pool.Storage.Api\Startup.cs 76 Active
Both of them not exist
You can try it:
Install-Package "YourProjectName" -Version 6.2.0
Answering my own question, for anyone:
VS-2019 corrected it's output folder as bin folder with a new bug-fix! but it was not related with the nuget package removement problem
How to remove a package from a project:
In Visual Studio i want to go to file with F12 key. And with the open target folder command i see the local folder of the removed package as
C:\Users\HAMIT\AppData\Local\Temp\MetadataAsSource\821e7bd605414042826501fe0b5cdc21\32ae5450ea724e84b62f983bdb6bb879
I have deleted this temp files but by the rebuild process VS restored them again. This some bored me and i found a new manner. There is always a technic to removing a package from the VisualStudio with the -force argument like below in PM Console
Uninstall-Package -ProjectName "YourProjectName" -RemoveDependencies -Force
When you put this command it will asking you the PackageId will be removed. Write your package name and it is ok!
If you dont know your package name exactly then you can get a list according your project like below:
Get-Package -ProjectName "YourProjectName"
But uninstalling to StackExchange.Redis.StrongName still is enigma!!!
UPDATE: I found a solution for this too! Change Aspnetcore framework 2.2 to 3.0 under Visual Studio 2019. Now seem to right dlls and Everything corrected.
I am trying to write some unit tests in C# in a '.NETFramework,Version=v4.5.2' application but all tests give the next error:
'System.IO.FileNotFoundException : Could not load file or assembly
'System.Drawing.Common, Version=0.0.0.0, Culture=neutral,
PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file
specified.'
When I try to install System.Drawing.Common I get the next error from the NuGet package:
Could not install package 'System.Drawing.Common 4.5.1'. You are trying to install this package into a project that targets
'.NETFramework,Version=v4.5.2', but the package does not contain any assembly references or content files that are compatible with that framework. For more
information, contact the package author.
I cannot change the application version or the framework (.NET Core 2.1) and any other trick I found online did not work (or generated more errors).
Help?
in NuGet put this line :
Install-Package System.Drawing.Common -Version 4.5.2
in .NET CLI put :
dotnet add package System.Drawing.Common --version 4.5.2
in Paket CLI put :
paket add System.Drawing.Common --version 4.5.2
Had the same problem. I have cloned solution https://github.com/barnhill/barcodelib . It has two projects: library project targets .Net Standard 2.0 and refers to System.Drawings.Common. Example project depends on library and has reference to System.Drawings.Common. Example project was not compiling due to same error.
My solution was just to remove reference to System.Drawings.Common in nuget packages and readd it (rclick on Example project > Manage nuget packages > Browse Installed, remove the System.Drawings.Common package, and then add it back), unload project and then reload it again
Helped for me.
I managed to solve it by restarting Visual Studio, changing the framework to 4.6.1 (which I could not do before) and adding the reference.
OP's solution migrated from the question to an answer.
you could try to use one of the libs described in here instead https://devblogs.microsoft.com/dotnet/net-core-image-processing/
you could try to use https://www.nuget.org/packages/CoreCompat.System.Drawing/ as well and maybe try to change your app framework to .net standard 2.0?
This is kind of bizarre but it worked dramatically so I'm going to mention it. I built a small vs 2017 ent console project that was supposed to read an oracle database. When I started to run it I got the System.Drawing.Common error mentioned above. It seemed stupid because I wasn't really doing anything having to do with Drawing at all. In Manage Nuget Packages I deleted the Oracle.ManagedDataAccess driver and added the Oracle.ManagedDataAccess.Core and the System.Drawing.Common error went away and I was able to read my oracle database. So I'm suggesting that with NuGet you may be picking up some things you really don't need and if you have any choice for your NuGet packages, try different ones. This also may be some foible with how my organization managed nuget for Visual Studio 2017 enterprise.
I'm creating a .NET Core xUnit test project library in VS2017. I accidentally installed the System.Collections.Immutable NuGet package, and now I want to uninstall it.
First, I tried editing the csproj file and removing this line:
<PackageReference Include="System.Collections.Immutable" Version="1.3.1" />
That didn't work, since the types from that package were still highlighted in the editor. So I tried running Uninstall-Package System.Collections.Immutable in the Package Manager Console.
The console said it couldn't find the package, so I tried putting the <PackageReference... line above back and running Uninstall-Package again. The console said the uninstall succeeded, but VS still recognized the types.
I tried closing VS, deleting .vs/, and reopening. It still recognized the types.
I tried git stashing my changes, running git clean -xdf, and running dotnet restore from the command line. Somehow, it still outputs
$ dotnet restore
Restoring packages for C:\cygwin64\home\james\Code\cs\BlockList\src\BlockList\BlockList.csproj...
Restoring packages for C:\cygwin64\home\james\Code\cs\BlockList\src\BlockList.Tests\BlockList.Tests.csproj...
...
Installing System.Collections.Immutable 1.2.0.
Installing System.Collections.Immutable 1.3.0.
...
I also tried restoring from Visual Studio instead of the command line. Still no luck.
When I searched all the files in my repo for the word Immutable, the only thing popping up is project.assets.json in the obj directory. Not a single source file contains the word Immutable. I'm confused, then, as to how it's still being referenced. How can I uninstall it?
A few other things:
I checked the 'Dependencies' drop-down for my project in Solution Explorer, and it's not listed there.
I don't want to delete my local copy of the repo and re-clone it, since I have other work stashed.
System.Collections.Immutable.dll doesn't show up at all in the bin/ directory, yet when I use one of the types in my library and run it, it works fine.
Thanks!
edit: Adding this line to the library:
System.Diagnostics.Debug.WriteLine(typeof(ImmutableArrayExtensions).GetTypeInfo().Assembly.Location);
And running it says that the assembly is located in this location:
C:\Program Files\dotnet\shared\Microsoft.NETCore.App\1.1.2\System.Collections.Immutable.dll
After about an hour, I found out what the problem was. My .NET Core library referenced Microsoft.NETCore.App, which referenced System.Collections.Immutable. I looked at the dependencies for the former, but overlooked System.Collections.Immutable because that package references maybe 50 other packages. I ended up finding this out by creating a brand-new xUnit test project, and trying to see if ImmutableArray<> was present without installing anything. Sure enough, I could use it out of the box.
I had a similar issue with VS2017 and a different nuget package that wouldn't fully uninstall. I ultimately had to create a new project and cut and paste all my code into it. Kind of a sledge hammer approach, but faster than crawling through dependencies.
An update occurred last night and now I find myself unable to do a ctrl + '.' for code suggestions in VS 2015. An error message comes up saying the following:
Could not load file or assembly 'Microsoft.CodeAnalysis, version= 1.3.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.
I can still build and develop but this will be really annoying without this feature. I admit it, I am getting soft!
Anyone have a suggestion for fixing this bug?
As pointed out by #CaptainAmerica the solution is to update the CodeDom assembly from NuGet. One should point out how to do this in Visual Studio.
I found the solution here:
https://www.nuget.org/packages/Microsoft.CodeDom.Providers.DotNetCompilerPlatform/
Basically, in the Visual Studio menu select:
Tools-> Nuget Package Manager -> Package Manager Console
In the console that appears at the bottom of Visual Studio run this command:
Install-Package Microsoft.CodeDom.Providers.DotNetCompilerPlatform
If any of the options doesn't work, here is the detailed guide to handle this scenario....
First of all version is important. Notice the version mentioned in the error...
Could not load file or assembly 'Microsoft.CodeAnalysis, version= 1.3.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.
If you see above error it means that it's not able to find version 1.3.1. Now please create new VS project. No preferences, it can be just simple console application. Now once project template is ready, go to Package Manager and run following command with your specified version...
Install-Package Microsoft.CodeAnalysis -Version 1.3.1
This will install all packages. Let it complete. Once it's done. We don't need this newly created project at all. You can delete it completely. Seriously! you can delete it. We did this because we wanted that package installed at global nuget level. When you install something, nuget stores it at global level of your machine as well. Path will be something like this...
C:\Users\<<Your Windows User>>\.nuget\packages
You can know your path by following...
%USERPROFILE%\.nuget\packages
Now you will see your required Microsoft.CodeAnalysis.dll there in following folder...
C:\Users\<<Your Windows User>>\.nuget\packages\Microsoft.CodeAnalysis.Common\1.3.1\lib\net45
Please note that above path contains version number (1.3.1). If your version is different, look into that version folder.
Now that you have dll with you, all that you need to do is add that dll to GAC. For that you will need GacUtil.exe
This file get installed along with Visual Studio already. You can search "GacUtil" in C drive. For me it's there on below path...
C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools
Now run command prompt as Administrator and navigate current directory to your path containing GacUtil. The run below command to install that dll in GAC.
gacutil
-i C:\Users\<<You Windows User>>\.nuget\packages\Microsoft.CodeAnalysis.Common\1.3.1\lib\net45\Mi
crosoft.CodeAnalysis.dll
Basically providing path to dll we installed globally. It will show message on successful installation.
That's all! Now start your Visual Studio again and you will get this issue fixed.
I had the same problem with Visual Studio 2015 Update 2, to solve the problem globally for all solutions, update to Visual Studio 2015 Update 3. Here is a link: Download from here
I found this page and at the bottom of the page under "Compatibility with ASP.NET" it says that ASP.NET uses the nuget package Microsoft.CodeDom.Providers.DotNetCompilerPlatform to update the Microsoft.CodeAnalysis assemblies. So I updated the DotNetCompilerPlatform package from 1.0.0 to 1.0.3 and it works again! Yeah, life is good again!
To solve this problem for all solutions, I needed to install the assembly Microsoft.CodeAnalysis.dll directly to the GAC – Global Assemblies Cache.
I got the file Microsoft.CodeAnalysis.dll from C:/Users/[user]/.nuget/packages/Microsoft.CodeAnalysis.Common/1.3.2/lib/portable-net45+win8/Microsoft.CodeAnalysis.dll.
To install the assembly to the GAC, I used a PowerShell script from https://github.com/LTruijens/powershell-gac.
Finally, I managed to install the assembly to the GAC with the following command from PowerShell:
Add-GacAssembly [myPath]\Microsoft.CodeAnalysis.dll
I was also facing same issue. My visual studio 2015 version was 2.0. I upgraded it to version 3.
Issue got solved !!!
I was also facing same issue, Try installing Microsoft.CodeAnalysis.CSharp nuget package. And if its not resolved, check version of Microsoft.Net.Compilers in your project, I was having version as 1.0.0 which was causing issue so updated Microsoft.Net.Compilers nuget to v1.3.2.
Found solution here : Github discussion
I have solved the problem this way:
Update Visual studio 2015 to Update 3
from the menu chose View => Notifications => Visual Studio Update 3 and clicked the update button.
Today ! i got the same problem and i resolve it by restarting visual studio 2015 :)
I encountered this issue when trying to build someone's code from github.
I was opening it in VS 2019 but I also have VS 2022. Opening and building in VS 2022 just worked.
I fixed this issue by updating all Nuget Microsoft dependencies to v2.0.0. This was using VS 2017. I was using a preview version.
In my case, the error occurred after turning off Live Unit Testing and running tests manually.Some tests would fail with the above error message.
Going into Test --> Live Unit Testing --> Options and issuing "Delete Persisted Data" resolved the problem.
I fixed similar issue by deleting the .vs folder located inside the solution.
I found the missing assemblies in the NuGet package (After the first one was resolved there were mulitple otheres): https://www.nuget.org/packages/Microsoft.Net.Compilers/1.3.1-rc
Installed them using GacUtil from the Dev console.
Unfortunately the extensive solution with "nuget install/gacutil" here above couldn't work as the exact version of the dependency dll was not available.
The following solved my issue in our complex webform application was to perform within the solution in Visual Studio -> Tools -> Nuget -> console package manager
Update-Package -reinstall
In my case, I was getting this error because my Visual Studio Solution was trying to use TextTransform.exe during a pre-build step in one of the projects, but my local machine didn't have the TextTransform.exe at the path specified by my pre-build step and my first attempt at fixing that was to just copy TextTransform.exe from some other location to where Visual Studio expected it to be. That appears to have caused this problem, because TextTransform.exe has other silent dependencies.
My fix was to change the pre-build steps so instead of looking for the file at
C:\Program Files (x86)\Common Files\microsoft shared\TextTemplating\14.0\TextTransform.exe
it would instead look in the Visual Studio install folder which had a bunch of other files, presumably one of which was a peer dependency of TextTransform.exe
C:\Program Files\Visual Studio\Common7\IDE\TextTransform.exe
That seemed to fix things for me.
I got the same problem, I am using VS2017 and installed Microsoft.EntityFrameworkCore.SqlServer 3.1.8, while in my VS2017 .Net core version is 2.1
So I downgraded my Microsoft.EntityFrameworkCore.SqlServer to 2.1.1 and restarted the solution.
The warning was gone !!
I had similar errors in VS2019 after upgrading project to .Net 5 according to https://learn.microsoft.com/en-us/aspnet/core/migration/31-to-50?view=aspnetcore-5.0&tabs=visual-studio.
error CS0006: Metadata file
'C:\Users\.nuget\packages\microsoft.codeanalysis.analyzers\1.1.0\analyzers\dotnet\cs\Microsoft.CodeAnalysis.Analyzers.dll'
could not be found.
'C:\Users\.nuget\packages\microsoft.codeanalysis.analyzers\1.1.0\analyzers\dotnet\cs\Microsoft.CodeAnalysis.CSharp.Analyzers.dll' could not be founderror CS0006: Metadata file
The fix was to close and reopen VS 2019
Try adding Nuget Package Microsoft.VisualStudio.Web.CodeGeneration.Design to your project
This question is many years old and the answers are all fairly old as well. I only recently ran into a similar-enough issue that I feel sharing my experience here (although potentially not complete) is relevant.
D:\Code\C#\sdkrazortest>dotnet --version
6.0.401
D:\Code\C#\sdkrazortest>dotnet new webapp -o webapp1
The template "ASP.NET Core Web App" was created successfully.
This template contains technologies from parties other than Microsoft, see https://aka.ms/aspnetcore/6.0-third-party-notices for details.
Processing post-creation actions...
Running 'dotnet restore' on D:\Code\C#\sdkrazortest\webapp1\webapp1.csproj...
Determining projects to restore...
Restored D:\Code\C#\sdkrazortest\webapp1\webapp1.csproj (in 102 ms).
Restore succeeded.
D:\Code\C#\sdkrazortest>cd webapp1
D:\Code\C#\sdkrazortest\webapp1>dotnet build
MSBuild version 17.3.1+2badb37d1 for .NET
Determining projects to restore...
All projects are up-to-date for restore.
REWRITECSS : error : [D:\Code\C#\sdkrazortest\webapp1\webapp1.csproj]
An assembly specified in the application dependencies manifest (rzc.deps.json) was not found:
package: 'Microsoft.CodeAnalysis.Common', version: '4.3.0-3.22415.1'
path: 'lib/netcoreapp3.1/Microsoft.CodeAnalysis.dll'
Build FAILED.
REWRITECSS : error : [D:\Code\C#\sdkrazortest\webapp1\webapp1.csproj]
0 Warning(s)
1 Error(s)
Time Elapsed 00:00:10.32
I originally tried loading packages like the top comments here had suggested and realized this issue started happening shortly after install .net core 6. After seeing some SO comments on dotnet-aspnet-codegenerator being version-specific I tried to create my netcore3.1 app using an older version of the dotnet cli via global.json:
D:\Code\C#\sdkrazortest>type global.json
{
"sdk": {
"version": "3.1.403"
}
}
D:\Code\C#\sdkrazortest>dotnet new webapp -o webapp2
Getting ready...
The template "ASP.NET Core Web App" was created successfully.
This template contains technologies from parties other than Microsoft, see https://aka.ms/aspnetcore/3.1-third-party-notices for details.
Processing post-creation actions...
Running 'dotnet restore' on webapp2\webapp2.csproj...
Determining projects to restore...
Restored D:\Code\C#\sdkrazortest\webapp2\webapp2.csproj (in 187 ms).
Restore succeeded.
D:\Code\C#\sdkrazortest>cd webapp2
D:\Code\C#\sdkrazortest\webapp2>dotnet build
Microsoft (R) Build Engine version 16.7.0+7fb82e5b2 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.
Determining projects to restore...
All projects are up-to-date for restore.
webapp2 -> D:\Code\C#\sdkrazortest\webapp2\bin\Debug\netcoreapp3.1\webapp2.dll
webapp2 -> D:\Code\C#\sdkrazortest\webapp2\bin\Debug\netcoreapp3.1\webapp2.Views.dll
Build succeeded.
0 Warning(s)
0 Error(s)
Time Elapsed 00:00:09.91
While this isn't an answer for the new templates not just working for me.. it might help someone potentially get "unstuck" or be the catalyst for someone providing a proper answer.
You need to install NuGet package Microsoft.Net.Compilers.Toolset, but make sure you match the right .NET version. In my case I was targeting netcoreapp3.1 so max version could be 4.0.1.
My root cause was for MassTransit analyzers nagging they cant find Microsoft.CodeAnalysis package, eg. An instance of analyzer MassTransit.Analyzers.AsyncMethodAnalyzer cannot be created.
I think one of the possible reasons for this issue could be the incompatibility of the .NET Framework and certain NuGet package. In my case I have .NET Core 3.1 and Testcontainers 2.4.0. The error does not happen on my local machine but it keeps showing up in my GitHub Actions remotely. It goes away after I downgrade Testcontainers to 2.3.0. Later the NuGet package AspNetCore.HealthChecks.EventStore 6.0.2 causes the same issue but that gets fixed in minutes rather than in hours :)