现象:
本地可以正常编译和使用,用 Azure DevOps 进行 CI 时出错
重现:
项目代码:
<Project Sdk="Microsoft.NET.Sdk.Razor"> <PropertyGroup> <TargetFrameworks>netcoreapp3.1</TargetFrameworks> ...
YAML 配置:
trigger: - master pool: vmImage: 'windows-latest' variables: solution: '**/Senparc.Xncf.WeixinManager.sln' buildPlatform: 'Any CPU' buildConfiguration: 'Release' steps: - task: UseDotNet@2 displayName: 'Use .NET SDK 3.1' inputs: version: '3.1.x' packageType: 'sdk' - task: DotNetCoreCLI@2 displayName: Build inputs: command: build projects: '**/Senparc.Xncf.WeixinManager.sln' arguments: '--configuration Release'
编译日志记录:
Starting: Build ============================================================================== Task : .NET Core Description : Build, test, package, or publish a dotnet application, or run a custom dotnet command Version : 2.187.0 Author : Microsoft Corporation Help : https://docs.microsoft.com/azure/devops/pipelines/tasks/build/dotnet-core-cli ============================================================================== C:Windowssystem32chcp.com 65001 Active code page: 65001 Info: .NET Core SDK/runtime 2.2 and 3.0 are now End of Life(EOL) and have been removed from all hosted agents. If you're using these SDK/runtimes on hosted agents, kindly upgrade to newer versions which are not EOL, or else use UseDotNet task to install the required version. C:hostedtoolcachewindowsdotnetdotnet.exe build D:a1ssrcSenparc.Xncf.WeixinManager.sln "-dl:CentralLogger,"D:a\_tasksDotNetCoreCLI_5541a522-603c-47ad-91fc-a4b1d163081b2.187.0dotnet-build-helpersMicrosoft.TeamFoundation.DistributedTask.MSBuild.Logger.dll"*ForwardingLogger,"D:a\_tasksDotNetCoreCLI_5541a522-603c-47ad-91fc-a4b1d163081b2.187.0dotnet-build-helpersMicrosoft.TeamFoundation.DistributedTask.MSBuild.Logger.dll"" --configuration Release Microsoft (R) Build Engine version 16.10.2+857e5a733 for .NET Copyright (C) Microsoft Corporation. All rights reserved. Determining projects to restore... Restored D:a1ssrcSenparc.Xncf.WeixinManagerSenparc.Xncf.WeixinManager.csproj (in 1.12 min). Restored D:a1ssrcSenparc.Xncf.WeixinManager.TestsSenparc.Xncf.WeixinManager.Tests.csproj (in 1.12 min). ##[warning]C:hostedtoolcachewindowsdotnetsdk5.0.302SdksMicrosoft.NET.Sdk.Razoruild etstandard2.0Sdk.Razor.CurrentVersion.targets(416,5): Warning RAZORSDK1006: Detected Razor language version downgrade. This is typically caused by a reference to the Microsoft.AspNetCore.Razor.Design package. Consider removing this package reference. C:hostedtoolcachewindowsdotnetsdk5.0.302SdksMicrosoft.NET.Sdk.Razoruild etstandard2.0Sdk.Razor.CurrentVersion.targets(416,5): warning RAZORSDK1006: Detected Razor language version downgrade. This is typically caused by a reference to the Microsoft.AspNetCore.Razor.Design package. Consider removing this package reference. [D:a1ssrcSenparc.Xncf.WeixinManagerSenparc.Xncf.WeixinManager.csproj] ##[warning]srcSenparc.Xncf.WeixinManagerModelsDatabaseModelDtoWeixinUserDto.cs(63,23): Warning CS0108: 'WeixinUserDto.Remark' hides inherited member 'DtoBase.Remark'. Use the new keyword if hiding was intended. D:a1ssrcSenparc.Xncf.WeixinManagerModelsDatabaseModelDtoWeixinUserDto.cs(63,23): warning CS0108: 'WeixinUserDto.Remark' hides inherited member 'DtoBase.Remark'. Use the new keyword if hiding was intended. [D:a1ssrcSenparc.Xncf.WeixinManagerSenparc.Xncf.WeixinManager.csproj] ##[warning]srcSenparc.Xncf.WeixinManagerModelsDatabaseModelWeixinUser.cs(71,23): Warning CS0108: 'WeixinUser.Remark' hides inherited member 'EntityBase<int>.Remark'. Use the new keyword if hiding was intended. D:a1ssrcSenparc.Xncf.WeixinManagerModelsDatabaseModelWeixinUser.cs(71,23): warning CS0108: 'WeixinUser.Remark' hides inherited member 'EntityBase<int>.Remark'. Use the new keyword if hiding was intended. [D:a1ssrcSenparc.Xncf.WeixinManagerSenparc.Xncf.WeixinManager.csproj] ##[warning]srcSenparc.Xncf.WeixinManagerAreasAdminPagesWeixinManagerWeixinUserIndex.cshtml.cs(44,42): Warning CS1998: This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread. D:a1ssrcSenparc.Xncf.WeixinManagerAreasAdminPagesWeixinManagerWeixinUserIndex.cshtml.cs(44,42): warning CS1998: This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread. [D:a1ssrcSenparc.Xncf.WeixinManagerSenparc.Xncf.WeixinManager.csproj] ##[warning]srcSenparc.Xncf.WeixinManagerRegister.cs(189,13): Warning CS0162: Unreachable code detected D:a1ssrcSenparc.Xncf.WeixinManagerRegister.cs(189,13): warning CS0162: Unreachable code detected [D:a1ssrcSenparc.Xncf.WeixinManagerSenparc.Xncf.WeixinManager.csproj] ##[warning]srcSenparc.Xncf.WeixinManagerRegister.Middleware.cs(83,46): Warning CS0168: The variable 'ex' is declared but never used D:a1ssrcSenparc.Xncf.WeixinManagerRegister.Middleware.cs(83,46): warning CS0168: The variable 'ex' is declared but never used [D:a1ssrcSenparc.Xncf.WeixinManagerSenparc.Xncf.WeixinManager.csproj] ##[warning]srcSenparc.Xncf.WeixinManagerAreasAdminPagesWeixinManagerWeixinUserIndex.cshtml.cs(28,21): Warning CS0414: The field 'WeixinUser_IndexModel.pageCount' is assigned but its value is never used D:a1ssrcSenparc.Xncf.WeixinManagerAreasAdminPagesWeixinManagerWeixinUserIndex.cshtml.cs(28,21): warning CS0414: The field 'WeixinUser_IndexModel.pageCount' is assigned but its value is never used [D:a1ssrcSenparc.Xncf.WeixinManagerSenparc.Xncf.WeixinManager.csproj] ##[warning]CSC(0,0): Warning AD0001: Analyzer 'Microsoft.AspNetCore.Mvc.Analyzers.TopLevelParameterNameAnalyzer' threw an exception of type 'System.NullReferenceException' with message 'Object reference not set to an instance of an object.'. CSC : warning AD0001: Analyzer 'Microsoft.AspNetCore.Mvc.Analyzers.TopLevelParameterNameAnalyzer' threw an exception of type 'System.NullReferenceException' with message 'Object reference not set to an instance of an object.'. [D:a1ssrcSenparc.Xncf.WeixinManagerSenparc.Xncf.WeixinManager.csproj] It was not possible to find any compatible framework version The framework 'Microsoft.NETCore.App', version '2.0.9' was not found. - The following frameworks were found: 5.0.8 at [C:hostedtoolcachewindowsdotnetsharedMicrosoft.NETCore.App] You can resolve the problem by installing the specified framework and/or SDK. The specified framework can be found at: - https://aka.ms/dotnet-core-applaunch?framework=Microsoft.NETCore.App&framework_version=2.0.9&arch=x64&rid=win10-x64 ##[error]C:UsersVssAdministrator.nugetpackagesmicrosoft.aspnetcore.razor.design2.2.0uild etstandard2.0Microsoft.AspNetCore.Razor.Design.CodeGeneration.targets(79,5): Error : rzc discover exited with code -2147450730. C:UsersVssAdministrator.nugetpackagesmicrosoft.aspnetcore.razor.design2.2.0uild etstandard2.0Microsoft.AspNetCore.Razor.Design.CodeGeneration.targets(79,5): error : rzc discover exited with code -2147450730. [D:a1ssrcSenparc.Xncf.WeixinManagerSenparc.Xncf.WeixinManager.csproj]
尝试解决方案:
- 项目文件设置添加:<RazorLangVersion>3.0</RazorLangVersion> ,无效,引发其他错误:MSB4044 未给任务“ComputeCssScope”的必需参数“TargetName”赋值。
- 修改项目文件配置 Microsoft.NET.Sdk.Razor 为 Microsoft.NET.Sdk,集成后无法读取 Razor 页面
- 项目添加引用 <PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="3.1.1" />,无效
最终解决方案:
在 .csproj 文件中添加配置:
<AddRazorSupportForMvc>true</AddRazorSupportForMvc>