Package restore failed. Rolling back package changes VS2019 - asp.net-core

The package restore failed. Rolling back package changes in VS2019 for asp.net core.
When,I try to add API Controller using EF.
This didn't work for me
-Try to Clear All Nuget Caches ,
-Try clearing the ComponentModelCache
Can't Add View from Controller in VS 2015 : "There was an error running the selected code generator"
It does not solve this error.
can anyone help me?

I had the same issue while adding the view for an IActionResult in .net 5.0 MVC using VS2019, before updating all the installed packages.
For me, restarting the Visual Studio works.
After restart I tried again, auto scaffolding installed this package "Microsoft.VisualStudio.Web.CodeGeneration.Design" along with generating a View.

For EF Core packages version 5.0.5 I was only able to solve this using the command line tool. See answer by johanjuulj here Visual Studio error: There was an error running the selected code generator
dotnet aspnet-codegenerator controller -m MyModel -dc MyDbContext -name MyModelssController -async

1 - Go to Tools
2 - Options
3- NuGet Package Manager
4- General
5 - Clear All NuGet Cache(s)

There are a couple of reasons that might account for this behaviour. Some of the reasons, as I have experienced on different occasions, include but might not be limited to the following:
Outdated Visual Studio Environment: In this case, you might attempt to update your version of VS.
Outdated Packages: In this case, update some or all of the packages in your project. To do this, go to Tools -> Nuget Package Manager -> Manage Nuget Package for Solution. Choose the packages that you want to update. if you are not sure, check Select all packages

In my case I needed to create an API controller and the only solution that worked for me was to download from the visual studio installer, in the individual features section: all the features of the SKD.NET framework,
all the .NET Framework support packages, ASP.NET and web development tools, and a few SQL features that I need.

I tried some of the suggestions above- but in my case I had all the versions of EF aligned on version 5.0.9 except for the autogenerated Swashbuckle.
I removed the following:
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.9" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGenerators.Mvc" Version="6.0.9" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.3.0" />
then I had to reinstall (because it is used by my code):
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="6.0.9" />
Finally I ran the View scaffold again and now it worked!
I can see that during scaffolding the following package was auto installed:
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.10" />

Similar issue in VS 2022. Trying to add an API controller to a separate ASP.Net Core Web API project in a solution with a separate project containing the POCO and DBContext. Tried some of the same fixes above; no luck.
I added an Api folder to the project containing the POCO and DBContext and added an API controller there with no problem. Then I copied the controller class to the Controller folder in the API project. I fixed the Route attribute in the controller, removing the "api/" part, added a connection string to the appsettings.json and called builder.Services.AddDbContext... in the API project's Program.cs.
This worked for me.

I had the same problem and tried many of the solutions but they didn't work. My project used .Net Core 3.1 and what fixed the issue was updating all NuGet packages in my project to the latest version of 3.1.x
Hope this helps.

Related

.NET 7 dependencies and nuget packages managing

I've started a new .NET 7 project in Visual Studio 2022. The template I used was "ASP.NET Core WebAPI". The project turned out to look like this in the solution explorer:
I can see that there is a dependency upon a series of DLLs in the folder "C:\Program Files\dotnet\packs\Microsoft.AspNetCore.App.Ref\7.0.2\ref\net7.0". When I browse to this folder and grab, for instance, the "Microsoft.AspNetCore.Http.Abstractions.dll" assembly I see its version as 7.0.22..... which makes sense.
What is weird is that when I click "Manage NuGet Packages" on this project I see absolutely no NuGet package already installed. It looks like this dependency mechanism is something independent of NuGet.
Now I want to create a class library that will encapsulate some common functionality I'd like to share across the projects but it needs to read data from the HttpContext class, which is defined in the "Microsoft.AspNetCore.Http.Abstractions.dll" file. So how should I add this dependency?
Adding it through "dotnet add package Microsoft.AspNetCore.App" command on the class library projects seems like a waste of resources as it adds the entire bundle of dlls and I only care about .Http.Abstractons.dll and it's direct dependencies.
Plus, when I ran the command Visual Studio complained with the warning:
NETSDK1080 A PackageReference to Microsoft.AspNetCore.App is not necessary when targeting .NET Core 3.0 or higher. If Microsoft.NET.Sdk.Web is used, the shared framework will be referenced automatically. Otherwise, the PackageReference should be replaced with a FrameworkReference".
Adding the dll through NuGet worked as Visual Studio was not complaining any more but the version of assembly added was 2.2.0 and not 7.0.2 as in the dotnet package. So technically, the HttpContext referenced in one projects is a different thing to the HttpContext referenced in the other project.
Please help me understand this mechanism and how should I add the dll of interest to my project to be able to access HttpContext in my library.
When should I use dotnet add package and when should I use NuGet packages management? Any good reading on this subject to bring me up to speed from .NET Framework 4.+ to .NET 7 in this area?
As .NET user for the last 10 years or so I feel so lost in the recent developments and find official docs I can find on the web of little use.
I tried adding the Microsoft.AspNetCore.App package through the "dotnet add package" command - Visual Studio complained, plus it pulled the entire bundle of assemblies but I care about only "Microsoft.AspNetCore.Http.Abstractions.dll".
I tried adding the "Microsoft.AspNetCore.Http.Abstractions" NuGet package but the version of assembly added was completely different to the one referenced in the WebAPI project.
What is weird is that when I click "Manage NuGet Packages" on this project I see absolutely no NuGet package already installed.
Those dependencies are determined by the project SDK which can be found in the root element of .csproj:
<Project Sdk="Microsoft.NET.Sdk.Web">
...
</Project>
So how should I add this dependency?
For latest versions of .NET you should reference corresponding SDK via FrameworkReference, for example to reference ASP.NET Core components you should add <FrameworkReference Include="Microsoft.AspNetCore.App"/> to library projects .csproj file as mentioned in the docs (and in the warning):
<Project Sdk="Microsoft.NET.Sdk">
<!--... rest of file-->
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
<!--... rest of file-->
</Project>

.NET Core MVC Page Not Refreshing After Changes

I'm building a .NET Core MVC on the latest version 2.2. I have a problem when I make changes to the CSHTML file and refresh the page, my changes are not reflected in the browser. I have to restart the project in order to see my changes. This has been happening for a while now so I'm not exactly sure what change caused this issue.
I've tried using the chrome's "Empty Cache and Hard Reload" as well as other browsers to no avail. This happens on Windows and Mac using both Visual Studio for Mac and VS Code
In a default .Net Core project it works fine so it must be something in my project that changed along the way. I'm wondering where I need to start in order to debug this issue? I've tried commenting out almost everything in my Startup.csand Program.cs with no resolution.
In ASP.NET Core 3.0 and higher, RazorViewEngineOptions.AllowRecompilingViewsOnFileChange is not available.
Surprised that refreshing a view while the app is running did not work, I discovered the following solution:
Add Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation NuGet package to the
project
Add the following in Startup.cs:
services.AddControllersWithViews().AddRazorRuntimeCompilation();
Here's the full explanation for the curious.
There was a change made in ASP.NET Core 2.2 it seems (and I can't find any announcements about this change). If you are not explicitly running in the 'Development' environment then the Razor Views are compiled and you will not see any changes made to the .cshtml
You can however turn off this using some config in your Startup class as follows.
services.AddMvc().AddRazorOptions(options => options.AllowRecompilingViewsOnFileChange = true);
For ASP.NET Core 3.0 and higher, see Alexander Christov's answer.
I've just created a new project using the latest ASP.NET MVC Core 3.1 template and I altered the following to get runtime recompilation enabled for Debug:
Reference NuGet package - Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.
Startup.cs - ConfigureServices(IServiceCollection services) WAS:
// stuff...
services.AddControllersWithViews();
// more stuff...
NOW:
// stuff...
var mvcBuilder = services.AddControllersWithViews();
#if DEBUG
mvcBuilder.AddRazorRuntimeCompilation();
#endif
// more stuff...
In addition to Alexander Christov's answer, since ASP.NET Core 3.1 you can enable the view compilation for development environment without changes to the Startup file:
Install the Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation NuGet package.
Set next environment variables (for example via environmentVariables section in launchSettings.json):
ASPNETCORE_ENVIRONMENT to "Development".
ASPNETCORE_HOSTINGSTARTUPASSEMBLIES to "Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation".
You should just add this:
services.AddControllersWithViews();
to the ConfigureService method.
Be aware below code is not available in ASP.NET Core 3.1:
services.AddControllersWithViews().AddRazorRuntimeCompilation();
For those using Net core 3.0 or greater
Go to Tools → Nuget package manager → Manage nuget pakages for solution
move to browse tab to browse from internet
search for RuntimeCompilation
Click on Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation
install it on your intended projects the current stable version
open Startup.cs file
go to void method ConfigureServices
add line: services.AddControllersWithViews().AddRazorRuntimeCompilation();
you are DONE
Rerun and see. Now you can refresh your views or pages.
first of all install the Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation using nuget manager
after that add below code into your startup.cs
services.AddRazorPages().AddRazorRuntimeCompilation();
net6.0 also this works.
builder.Services.AddRazorPages().AddRazorRuntimeCompilation();
Using .net core 2.2 running app with command dotnet watch run the project is restarted after every change
Below helped me when views were in separate project.
if(HostingEnvironment.IsDevelopment()){ // only in development (optional)
services.AddMvc().AddRazorOptions(o => {
o.FileProviders.Add(new PhysicalFileProvider(PATH_TO_PROJECT));
});
}
I had the same issue while working on a .NET 6 MVC Web App.
I installed Microsoft.AspNetCore.Mvc.Razor.Runtime.Compilation from NuGet Package Manger then added .AddRazorRuntimeCompilation(); after
builder.services.AddControllersWithViews();
so that it looks like this
builder.services.AddControllersWithViews().AddRazorRuntimeCompilation();
and it worked!
Hope this helped.
I was able to solve this problem in Rider by adding the ASPNETCORE_ENVIRONMENT=Development environment variable.
There are two ways to resolve this issue:
1. Check the permissions of folder in which your .sln file present.There may be a problem with file access permissions as Visual studio may not be to access files when IIS express server is running, so to reflect new .cshtml changes each time you need to restart the server,so I suggest edit the folder access permissions by:
Right click on folder->properties->security->click on edit button -> check all options->save.
Restart Visual studio to see changes.
If this does not work then use 2 option.
2.In your project in startup.cs file add this below line ConfigureServices() in method :
services.AddMvc().AddRazorOptions(options => options.AllowRecompilingViewsOnFileChange = true);
Are you absolutely sure you are using 2.2? Check your csproj because it might be this bug https://github.com/aspnet/Razor/issues/2466
You could try turning off RazorCompileOnBuild more info https://learn.microsoft.com/en-us/aspnet/core/razor-pages/sdk?view=aspnetcore-2.1#properties
I had a similar problem upgrading from .net Core 3 to .net 5.0
Problem was due to old dependency in Telerik controls that we could not change.
Fixed by changing references in the .csproj file
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="3.8.0" />
to
<PackageReference Include="Microsoft.CodeAnalysis" Version="3.8.0" />
(your version may be different)
In Visual Studio 2022 Preview, it seems there is an option called Hot Reload for this purpose.
It seems to be available in Visual Studio 2019 too.
With Hot Reload you can now modify your apps managed source code while
the application is running, without the need to manually pause or hit
a breakpoint. Simply make a supported change while your app is running
and in our new Visual Studio experience use the “apply code changes”
button to apply your edits.
https://devblogs.microsoft.com/dotnet/introducing-net-hot-reload/

Can't run .NET Core 2.1(.1) application on Windows Server 2016

I'm trying to run a .NET Core application on my Windows Server 2016 instance. It builds/runs fine on my Windows 10 machine.
First I'm doing dotnet publish and I copy the published site to the Windows Server instance. I followed this guide and installed both the hosting bundle as well as the latest SDK (2.1.3).
However when I try to dotnet myapp.dll i get the following error message:
It was not possible to find any compatible framework version
The specified framework 'Microsoft.AspNetCore.All', version '2.1.1' was not found.
- Check application dependencies and target a framework version installed at:
C:\Program Files\dotnet\
- Installing .NET Core prerequisites might help resolve this problem:
http://go.microsoft.com/fwlink/?LinkID=798306&clcid=0x409
- The .NET Core framework and SDK can be installed from:
https://aka.ms/dotnet-download
- The following versions are installed:
2.1.0 at [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
I can see clearly that indeed Microsoft.AspNetCore.All only has a 2.1.0 version located in Program Files\dotnet\shared. How do I get the correct 2.1.1 version?
Any guidance is highly appreciated.
Use this link to install 2.1.1 core sdk version:
https://github.com/dotnet/versions/tree/7a833dddfddc27f2074b755b94234a25b9757637/build-info/dotnet/product/cli/release/2.1
Looks like there's been some issue with publishing the Microsoft.AspNetCore.All package, and it is not set to automatically install with VS update/Core SDK install. At least not for me. The nuget package was also added just 10 hours ago with 0 downloads. So by default we're still stuck with 2.1.0. To fix this, the first thing I did was to check what the ASP.NET web app templates use (they keep changing the defaults, so if you have an old project you're updating, it's always handy to check changes in default templates too).
The fix for me was removing the Version parameter from the tag in the .csproj file, as is now done in the default template. Original:
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.1.1" />
</ItemGroup>
After modification:
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" />
</ItemGroup>
This will essentially resolve to the latest installed version, i.e. 2.1.0 for now. Most likely 2.1.1 will be installed in the later updates. You could also try installing the latest package manually, but I recommend to resorting to default when possible. This way it's less likely that future updates will break my system.
You have two options:
Install the 2.1.1 framework on the server (as mentioned in another answer)
Publish your app with all the required assets. You can do this with the following command:
dotnet publish --self-contained

There was an error running the selected code generator: Could not load file or assembly Microsoft.EntityFrameworkCore, version = 2.0.1.0

When I was creating a Controller and a View by MVC Controller with views, using Entity Framework I got an error.
The Error is:
There was an error running the selected code generator: 'Could not
load file or assembly Microsoft.EntityFrameworkCore, version =
2.0.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' the located assembly's manifest definition does not match the assembly reference
Creating MVC Controller with views, using Entity Framework:
How can I solve this problem?
I use Visual Studio Version 15.5.2 and version of Microsoft.AspNetCore.All is 2.0.0
I updated the Microsoft.AspNetCore.All to version 2.0.3 and now it does work right.
Go to NugetPackage Manager and update the Microsoft.AspNetCore.All package
For me none of the above solutions worked. I had to add Microsoft.EntityFrameworkCore and Microsoft.EntityFrameworkCore.Design packages even though I have Microsoft.AspNetCore.All package added to my project.
Edit project in VS 2017 and add these lines
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.0.3" />
Update your packages or it can happen when you have an older SDK. Download a new .NET Core SDK and runtime from www.microsoft.com/net
I had a similar issue but mine was something with the versioning of visual studio.
I went to Visual Studio Installer and it notified me that I had to restart my computer
I got this same error:
Running Preview 2019 and .NET Core 3.
I moved the Nuget pkg sources up in my list.
I was attemping to add a new Controller and I would get this error every time.
I also noticed that for some reason I chose not to set up SSL but I had the setting in my launchSettings.json.
I deleted the setting for ssl and built the app and ran it.
After the successful run I could add the Controller without the error.
Follow these Steps:-
From Tools
Select NuGet Package Manager
-> Manage NuGet Packages For Solution
click on ->Updates Select the checkbox Select All
->Select Your Project from right hand side -> Click on Upadate
Having the same error, after downloaded VS2019"PREVIEW" and then opened a core 3.0 project there I was able to scaffold content no errors at all..

Vulnerabilities in .NET Core, ASP.NET Core Could Allow Elevation of Privilege

In reference to this security advisory, I'm using Visual Studio Code and my csproj contains numerous affected packages.
Does .net core cli have an update or upgrade feature? I manually updated the packages using a text editor but that was a pain in the butt.
I tried downloading the latest nuget.exe [v4.1] to see if it could read the csproj but nope - it's expecting a packages.config which doesn't exist.
No the CLI was not affected, only the packages listed on the link you provided.