Unable to Upgrade VS Project from ASP.NET Core 2.1 to 2.2 [duplicate] - asp.net-core

This question already has answers here:
The current .NET SDK does not support targeting .NET Core 2.2. (SDK 2.2.202)
(5 answers)
Closed 3 years ago.
Running VS version 15.9.4, I followed Microsoft's instructions (I think), but I'm still having trouble upgrading an existing 2.1 project to the latest stable (2.2.3).
Here is my dotnet --info
And here is my project file:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<Configurations>Debug;Release;DEBUG_LOCALAPI</Configurations>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
</PropertyGroup>
<ItemGroup>
<Content Remove="appsettings.Development.json" />
<Content Remove="appsettings.json" />
</ItemGroup>
<ItemGroup>
<None Include="appsettings.Development.json" />
<None Include="appsettings.json" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="3.0.0" />
<PackageReference Include="WindowsAzure.Storage" Version="9.3.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\PublishingAPICommon\PublishingAPICommon.csproj" />
</ItemGroup>
</Project>
If I try to install 2.2.3 to that project from NuGet manager, this is the error message I get:
Severity Code Description Project File Line Suppression State
Error NETSDK1045 The current .NET SDK does not support targeting .NET
Core 2.2. Either target .NET Core 2.1 or lower, or use a version of
the .NET SDK that supports .NET Core 2.2. PublishingStorage C:\Program
Files\dotnet\sdk\2.1.502\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.TargetFrameworkInference.targets 137
And here is what I see in VS when I try to edit the project properties directly:
Things I have tried:
Re-starting VS
Re-starting my machine
Creating a project-scoped global.json file with 2.2.3 value

From #martin-ullrich on https://github.com/aspnet/AspNetCore.Docs/issues/11844
Go to https://dotnet.microsoft.com/download/dotnet-core/2.2 and
download the installer for the 2.2.105 version. Anything >= 2.2.200
will not work in VS 2017. Both 2.2.105 and 2.2.202 contain the latest
and greatest .NET Core 2.2 version but they differ in the version of
the tools they include and are based on - the 1xx train is MSBuild
15., NuGet 4. etc. and the 2xx train is MSBuild 16., NuGet 5. and so on and Visual Studio only looks for the latest version compatible with
its version of MSBuild - for VS 2017 this is 2.1.502 version which
cannot target .NET Core 2.2. (And to add more confusion, there are now
2.1.6xx versions with VS 2019 tool versions together with the .NET Core 2.1 runtime)
[#]leecow and [#]rowanmiller are working on updating that page
regarding VS, also see dotnet/core#2542 and dotnet/sdk#3076 which are
about the same problem.
My contribution: After installing 2.2.105, I deleted the folder(s) titled '2.2.202' in directory C:\Program Files (x86)\dotnet\sdk and/or C:\Program Files\dotnet\sdk so that dotnet --info now reads the following:

Related

Can't create EF migrations after installing .NET 6

I've just installed .NET 6 SDK and updated all my projects to use target net6.0, but when I try to create a new migration with the dotnet tool I get:
.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<ApplicationIcon>assets-logo.ico</ApplicationIcon>
<RootNamespace>LC.Assets</RootNamespace>
<Authors>Stein Lundbeck</Authors>
<Company>Lundbeck Consulting</Company>
<Product>LC Assets</Product>
<Copyright>2021</Copyright>
</PropertyGroup>
<ItemGroup>
<Content Include="assets-logo.ico" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\LC.Components.Core\LC.Components.Core.csproj" />
<ProjectReference Include="..\LC.Components\LC.Components.csproj" />
</ItemGroup>
</Project>
It was not possible to find any compatible framework version The framework 'Microsoft.NETCore.App', version '2.0.0' (x64) was not found.
The following frameworks were found:
6.0.0 at [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
You can resolve the problem by installing the specified framework and/or SDK.
I've updated the dotnet-ef tool to version 6.0.0, so I don't know what it is that still target the 2.0.0 version.
Tool 'dotnet-ef' was successfully updated from version '5.0.4' to version '6.0.0'.
Any ideas?
I just ran into the exact same issue.
Rather than installing Microsoft.NETCore.App 2.0.0., I installed the Entity Framework Core Design package:
dotnet add package Microsoft.EntityFrameworkCore.Design
It made the error go away for me.
Note: I came upon the solution after remembering that following the EF Getting Started guide (https://learn.microsoft.com/en-us/ef/core/get-started/overview/first-app?tabs=netcore-cli) had me install the Design package and didn't throw the error when I followed the guide.
I had this problem recently after upgrading to .NET6.
The solution for me was to follow the link in the error message and install Microsoft.NETCore.App 2.0.0.
https://dotnet.microsoft.com/download/dotnet/2.0/runtime
I'm guessing that the upgrade to 6 removed this framework as it is considered legacy ... BUT ... is still used (or marked as a required dependency somehow) in the dot net tools.
The solution for me was installing EntityFrameworkCore.Design as suggested by #Thomas Z.
dotnet add package Microsoft.EntityFrameworkCore.Design
As well, making sure the path of the db is correct when creating the ef migration. It won't work if the path is dynamically created.

Downgrade from Core 3 to 2 then Microsoft.AspNetCore.App not recognised

I have VS2019 v16.3.5 installed along with the dotnet core 3 sdk. I can browse to C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App and see lots of the versions of the shared framework in folders from 2.1.9 to 3.0.0
To reproduce the problem:
create new console app
edit project file to add:
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
At this point it works ok but then:
Change target framework to 2.2
I get the build error:
The FrameworkReference 'Microsoft.AspNetCore.App' was not recognized
Should it be possible to target 2.2 and still use the shared framework Microsoft.AspNetCore.App?
FrameworkReference is a new feature of ASP.NET Core 3.0. Not available in the lower versions. For lower versions replace <FrameworkReference Include="Microsoft.AspNetCore.App" /> with the following:
<PackageReference Include="Microsoft.AspNetCore.App" />

What is the default version of the nuget package when referenced with PackageReference in .NET Core project?

I am trying to learn and understand nuget and msbuild in .NET Core by examining and manually editing project files (.csproj in .NET Core 2.2).
So when I create WebApi project, the .csproj file looks like this:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
</ItemGroup>
</Project>
Notice that there is no Version attribute specified for the first PackageReference.
Now if I specify it to be the latest stable version 2.2.3 like this:
<PackageReference Include="Microsoft.AspNetCore.App" Version="2.2.3" />
I get build warning NETSDK1071 which says:
A PackageReference to 'Microsoft.AspNetCore.App' specified a Version
of 2.2.3. Specifying the version of this package is not recommended.
For more information, see https://aka.ms/sdkimplicitrefs
This warning is not shown when Version attribute is omitted so I was wondering how is nuget package Version resolved when not set explicitly?
Also, how does dotnet build knows which version of a nuget package is recommended with the current project settings?
From the link in the warning, you can learn that it is not a regular package, but Meta-package.
It's mean that this package depends on your TargetFramework, and this is mean that when you target to a specific framework that installed in your machine (as SDK), the package will be taken from the specific SDK.

How to add <frameworkAssembly> into nuget package generated from csproj

I'm using VS2017 and the new csproj file format for creating nuget packages.
In my csproj file I have the following:
<ItemGroup>
<Reference Include="System.Net" />
</ItemGroup>
Which works fine (TargetFrameworks == net45) when you build it. But when I pack it in nuget package I want the target package to have it as
<frameworkAssemblies>
<frameworkAssembly assemblyName="System.Net" targetFramework="net45" />
</frameworkAssemblies>
How can I do that with this new tooling?
This is limitation of the current 1.0.* tooling. In the upcoming versions 1.1.* and 2.0.* versions of the ".NET SDK", this will be done automatically, with all <Reference> elements being added as framework assemblies to the resulting NuGet package (unless they are marked with Pack="false"). These changes will also be part of VS 2017 15.3 (not released yet at the time of writing). Note that i am talking about the tools (dotnet --version with SDK installed) version, not the .NET Core runtime versions.
There is a way to use the current preview packages of the pack targets, overriding the ones provided by the SDK - note that this is quite a hacky way and should be removed once you use the new 1.1 or 2.0 tooling.
<Project>
<PropertyGroup>
<NuGetBuildTasksPackTargets>junk-value-to-avoid-conflicts</NuGetBuildTasksPackTargets>
</PropertyGroup>
<Import Project="Sdk.props" Sdk="Microsoft.NET.Sdk" />
<!-- All your project's other content here -->
<ItemGroup>
<PackageReference Include="NuGet.Build.Tasks.Pack" Version="4.3.0-preview1-4045" PrivateAssets="All" />
</ItemGroup>
<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" />
</Project>
Also see the related GitHub issue on the NuGet repo where the information for this workaround originated from.

.NET Core how to test with net461 and xunit

dotnet new xunit ->
dotnet restore ->
dotnet test
Total tests: 1. Passed: 1. Failed: 0. Skipped: 0.
Test Run Successful.
Test execution time: 1,7148 Seconds.
.csproj; change target framework to net461:
<PropertyGroup>
<TargetFramework>net461</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
<PackageReference Include="xunit" Version="2.2.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
</ItemGroup>
</Project>
then
dotnet restore ->
dotnet test
Starting test execution, please wait...
No test is available in C:\Projects\testing\bin\Debug\net461\testing.dll. Make sure that installed test discoverers & executors, platform & framework version sett
ings are appropriate and try again.
How am I supposed to test net461-projects with xunit?
I already have a big project I've upgraded from .NET Core 1.0, and testing worked fine before the upgrade, so changing test framework would require some work.
Update
As it turns out, this is probably not related to xunit and testing - ASP.NET Core projects targeting net461 won't run at all on my machine anymore, neither through VS or from cmd.
The project I am trying to run is an new empty web project from the VS template. The csproj looks like this:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net461</TargetFramework>
<RuntimeIdentifier>win7-x86</RuntimeIdentifier>
</PropertyGroup>
<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.1" />
</ItemGroup>
</Project>
The error I get is this:
dotnet run
Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.AspNetCore.Hosting, Version=1.1.1.0, Culture=neutral, PublicKeyTo
ken=adb9793829ddae60' or one of its dependencies. The system cannot find the file specified.
at WebApplication2.Program.Main(String[] args)
I have tried removing all traces of Visual Studio and .NET Core from my machine and reinstalling them, but the error is the same.
There is an issue with runtime identifier inference and how the test sdk works.
Try adding this to your <PropertyGroup> (assuming you're on a 64 bit windows):
<RuntimeIdentifier>win7-x64</RuntimeIdentifier>
you could also add
<OutputType>Exe</OutputType>
which would turn your class library project into an exe but this should work around the issue.
Yeah, I've experienced the same issue. Not sure if Microsoft already has a fix available, but it seems that some packages of ASP.NET Core 1.1.x are targeting NetStandard Library 1.6.1, which is not compatible with .NET Framework... See the matrix on MS website here: https://learn.microsoft.com/en-us/dotnet/articles/standard/library#net-platforms-support
We've decided to stick to ASP.NET Core 1.0.x for now.