使用.NET 8预览版6构建项目时出现CS9057错误求助
问题
基于**.NET 8.0.0-preview.6.16**版本创建新项目后,构建时出现以下错误:
Error CS9057 分析器程序集‘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’引用的编译器版本为4.7.0.0,比当前运行的版本4.6.0.0更高。
尝试手动更新Microsoft.NET.Sdk.Razor\source-generators至4.7.0.0版本,但未找到有效更新路径。
项目文件内容如下:
<PropertyGroup> <RootNamespace>House.Web</RootNamespace> <AssemblyName>House.Web</AssemblyName> </PropertyGroup> <ItemGroup> <ProjectReference Include="..\Application\Application.csproj" /> <ProjectReference Include="..\Infrastructure\Infrastructure.csproj" /> </ItemGroup> <ItemGroup> <PackageReference Include="Azure.Extensions.AspNetCore.Configuration.Secrets" /> <PackageReference Include="Azure.Identity" /> <PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" /> <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" /> <PackageReference Include="Microsoft.AspNetCore.OpenApi" /> <PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore" /> <PackageReference Include="NSwag.AspNetCore" /> <PackageReference Include="NSwag.MSBuild"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> </PackageReference> <PackageReference Include="FluentValidation.AspNetCore" /> <PackageReference Include="ZymLabs.NSwag.FluentValidation.AspNetCore" /> </ItemGroup> <!-- Auto-generated Open API specification and Angular TypeScript clients --> <PropertyGroup> <RunPostBuildEvent>OnBuildSuccess</RunPostBuildEvent> </PropertyGroup> <Target Name="NSwag" AfterTargets="PostBuildEvent" Condition=" '$(Configuration)' == 'Debug' And '$(SkipNSwag)' != 'True' "> <Exec ConsoleToMSBuild="true" ContinueOnError="true" WorkingDirectory="$(ProjectDir)" EnvironmentVariables="ASPNETCORE_ENVIRONMENT=Development" Command="$(NSwagExe_Net70) run config.nswag /variables:Configuration=$(Configuration)"> <Output TaskParameter="ExitCode" PropertyName="NSwagExitCode" /> <Output TaskParameter="ConsoleOutput" PropertyName="NSwagOutput" /> </Exec> <Message Text="$(NSwagOutput)" Condition="'$(NSwagExitCode)' == '0'" Importance="low" /> <Error Text="$(NSwagOutput)" Condition="'$(NSwagExitCode)' != '0'" /> </Target> </Project>
解决方案
- 升级.NET SDK版本:该错误由预览版SDK版本不匹配导致,下载安装高于
8.0.100-preview.6.23330.14的.NET 8预览版SDK,新版本会同步更新编译器与Razor生成器版本,解决版本冲突。 - 强制指定编译器版本:在项目文件的
<PropertyGroup>节点中添加以下配置,强制使用匹配的编译器版本:<LangVersion>preview</LangVersion> <RoslynVersion>4.7.0</RoslynVersion> - 清理重建项目:执行以下命令清理项目缓存后重新构建:
dotnet clean dotnet restore dotnet build - 统一依赖版本:确保所有
Microsoft.AspNetCore.*相关包引用使用与.NET 8预览版匹配的版本,可在<PackageReference>中明确指定版本号,例如:<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="8.0.0-preview.6.23329.7" />
内容的提问来源于stack exchange,提问作者Alaa Barakat
相关产品推荐
相关产品推荐

