2

I opened my Visual Studio 2022 project this morning and cannot get it to run any more due to the following error:

CS9057 The analyzer assembly 'C:\Program Files\dotnet\sdk\8.0.100-preview.6.23330.14\Sdks\Microsoft.NET.Sdk.Razor\source-generators\Microsoft.NET.Sdk.Razor.SourceGenerators.dll' references version '4.7.0.0' of the compiler, which is newer than the currently running version '4.6.0.0'."

I am currently using Microsoft Visual Studio Community 2022 (64-bit) Version 17.6.5

My project file:

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>     
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Components.Analyzers" Version="6.0.20" />
    <PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="6.0.16" />
    <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="6.0.16" />
    <PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="6.0.16" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Analyzers" Version="6.0.20" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.16" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.16" />
    <PackageReference Include="MudBlazor" Version="6.2.3" />
  </ItemGroup>

</Project>

I did recently install the Microsoft Visual Studio Community 2022 (64-bit) Version 17.7.0 Preview 3.0 that includes the .NET 8 SDK. This is what seems to be causing the analyzer reference conflict.

I have not been able to find documentation to aid me in changing my analyzer references back to the correct version and there does not seem to be any way to do it in the Nuget Package Manager or the Reference Manager.

My other .NET 6 projects work, just not this one.

I have tried cleaning the project and deleting the obj and bin folders and even deleting the vs folder.

output from dotnet --list-sdks:

3.0.103 [C:\Program Files\dotnet\sdk] 3.1.426 [C:\Program Files\dotnet\sdk] 7.0.306 [C:\Program Files\dotnet\sdk] 7.0.400-preview.23330.10 [C:\Program Files\dotnet\sdk] 8.0.100-preview.6.23330.14 [C:\Program Files\dotnet\sdk]

Output from dotnet --version:

8.0.100-preview.6.23330.14

How do I change the analyzer references back to .NET 6? or what is the best way to fix this problem?

Tyson Gibby
  • 2,590
  • 2
  • 18
  • 37
  • Because of time constraints I ended up uninstalling the VS 2022 preview for .NET 8 and the .NET 8 SDK, and to fully get rid of the issue I needed to then uninstall the current .NET SDK and reinstall it. The project now builds and runs again. We will install .NET 8 again at a later time after there is a resolution to this issue. – Tyson Gibby Aug 23 '23 at 17:02

5 Answers5

1

From Select the .NET version to use doc:

The SDK uses the latest installed version

SDK commands include dotnet new and dotnet run. The .NET CLI must choose an SDK version for every dotnet command. It uses the latest SDK installed on the machine by default, even if:

  • The project targets an earlier version of the .NET runtime.
  • The latest version of the .NET SDK is a preview version.

Try adding global.json file to the root of the project:

The global.json file allows you to define which .NET SDK version is used when you run .NET CLI commands. Selecting the .NET SDK version is independent from specifying the runtime version a project targets. The .NET SDK version indicates which version of the .NET CLI is used.

With something like:

{
  "sdk": {
    "version": "6.0.0",
    "rollForward": "latestMinor"
  }
}
Guru Stron
  • 102,774
  • 10
  • 95
  • 132
  • Thanks, after adding that to that global.json file to root of my project, it does generate an additional message `message NETSDK1057: You are using a preview version of .NET. See: https://aka.ms/dotnet-support-policy`; but I, unfortunately still get the same error afterward. – Tyson Gibby Jul 21 '23 at 20:28
  • @TysonGibby try clean-rebuild. If it does not help - manually delete bin and obj folders and restart VS. Also try building with `dotnet build` – Guru Stron Jul 21 '23 at 20:31
  • @TysonGibby also please share output of `dotnet --list-sdks` and `dotnet --version`. And update VS 2022 preview to latest (4th) version (and .NET 8 preview too - it is at 6th, if I remember correctly) – Guru Stron Jul 21 '23 at 20:32
  • Thanks again for your help. I have tried a clean rebuild and manually deleting the obj and bin folders as well as the vs folder, please excuse me for not having stated so. I have now also added the additional information you requested. – Tyson Gibby Jul 21 '23 at 20:45
  • @TysonGibby you are targeting `net6.0` but do not have .NET 6 SDK installed - install it (the best option I would say). Or change `version` in global json to `7.0.0` or `7.0.306`. – Guru Stron Jul 21 '23 at 20:46
  • Strange, I definitely did not uninstall it. Not sure how that would happened. Been using it for a long time now. And I have other projects that are .NET 6 that are unaffected. I did install the .NET 6 SDK again and I am still getting the same error. – Tyson Gibby Jul 21 '23 at 21:04
  • @TysonGibby can you post a [mre]? Also try rebooting =) – Guru Stron Jul 21 '23 at 21:05
  • 1
    Because of time constraints I ended up uninstalling the VS 2022 preview for .NET 8 and the .NET 8 SDK, and to fully get rid of the issue I needed to then uninstall the current .NET SDK and reinstall it. The project now builds again. – Tyson Gibby Jul 21 '23 at 21:25
0

Because of time constraints I ended up uninstalling the VS 2022 preview for .NET 8 and the .NET 8 SDK, and to fully get rid of the issue I needed to then uninstall the current .NET SDK and reinstall it. The project now builds and runs again.

We will install .NET 8 again at a later time after they have worked out these bugs.

I am not accepting this answer as it does not solve the problem.

Tyson Gibby
  • 2,590
  • 2
  • 18
  • 37
0

I had this issue on release version of VisualStudio 2022 and .NET 8 (v8.0.100-preview.6). To fix the issue i had to completely uninstall .NET 8, clean the project and restart Visual studio

Andrey Tagaew
  • 1,573
  • 1
  • 15
  • 20
0

There are a few solutions here you can try:

  1. Update Visual Studio 2022 to 17.7.1
  2. Install the release version of the 7.0.400 SDK, not the preview one
  3. Make sure you're running the latest version of the Visual Studio 2022 Preview (17.8.0 Preview 1)
  4. Install the 7.0.307 SDK instead and target that with a global.json file

#4 is what worked for me on Azure DevOps Pipelines. Microsoft is supposed to be rolling out a fix there, but it isn't in Production yet.

Robert McLaws
  • 2,258
  • 2
  • 19
  • 22
0

This error was caused in our build pipelines because they contained a UseDotNet@2 task to target a specific version of a .net sdk, but the actual build was running a VSBuild@1. The VSBuild@1 task did not respect the dotnet CLI UseDotNet@2 task and was just compiling using the latest sdk on the build server which is .net 7 at current instead of .net 6 that was requested by the @UseDotNet@2 task. We switched the VSBuild@1 task to a DotNetCoreCLI@2 publish task and everything worked as expected.