使用 C# 10 发布 ASP.NET Core 6 Web 应用程序时 <ImplicitUsings />
从 Visual Studio 2022 发布配置文件启用到 Azure 应用服务,发布构建失败,因为缺少 using
声明。
背景
C# 10 引入了新的 implicit usings功能,其中某些 using
指令被视为 global using
directives基于SDK。这可以通过以下 csproj
启用配置:
<PropertyGroup>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
这与本地构建完全符合预期。例如,当定位到 Microsoft.NET.Sdk.Web
时SDK,我可以删除 using
System
的指令, System.Collections.Generic
, System.Linq
, &C。发布错误
但是,从 Visual Studio 2022 发布配置文件发布到 Azure 应用服务时,生成输出显示如下错误:
C:\Code\MyWebApp\MyClass.cs(41,25): Error CS0246: The type or namespace name 'IEnumerable<>' could not be found (are you missing a using directive or an assembly reference?)
这可以在下面看到作为扩展上下文的一部分:Build started...
1>------ Build started: Project: MyWebApp, Configuration: Release Any CPU ------
1>Done building project "MyWebApp.csproj".
2>------ Publish started: Project: MyWebApp, Configuration: Release Any CPU ------
Determining projects to restore...
All projects are up-to-date for restore.
C:\Program Files\Microsoft Visual Studio\2022\Professional\MSBuild\Current\Bin\Roslyn\csc.exe /noconfig /unsafe- /checked- /nowarn:1701,1702,1701,1702,2008 /fullpaths /nostdlib+ /platform:x64 /errorreport:prompt /warn:6 /define:TRACE;RELEASE;NET;NET6_0;NETCOREAPP;NET5_0_OR_GREATER;NET6_0_OR_GREATER;NETCOREAPP1_0_OR_GREATER;NETCOREAPP1_1_OR_GREATER;NETCOREAPP2_0_OR_GREATER;NETCOREAPP2_1_OR_GREATER;NETCOREAPP2_2_OR_GREATER;NETCOREAPP3_0_OR_GREATER;NETCOREAPP3_1_OR_GREATER /errorendlocation /preferreduilang:en-US /highentropyva+ /reference: /debug+ /debug:portable /filealign:512 /optimize+ /out:obj\Release\net6.0\win-x64\MyWebApp.dll /refout:obj\Release\net6.0\win-x64\ref\MyWebApp.dll /target:exe /warnaserror- /utf8output /deterministic+ /langversion:10.0 /analyzerconfig:… /analyzer:"C:\Program Files\dotnet\sdk\6.0.100\Sdks\Microsoft.NET.Sdk.Web\analyzers\cs\Microsoft.AspNetCore.Analyzers.dll" /additionalfile:Areas\MyArea\Index.cshtml … /warnaserror+:NU1605
C:\Code\MyWebApp\MyClass.cs(41,25): Error CS0246: The type or namespace name 'IEnumerable<>' could not be found (are you missing a using directive or an assembly reference?)
2>Build failed. Check the Output window for more details.
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
========== Publish: 0 succeeded, 1 failed, 0 skipped ==========
我认为这与 csc.exe
有关。发布配置文件生成的命令。发布简介
不幸的是,如何在我的
pubxml
中解决这个问题对我来说并不是很明显。文件,这是非常直接的:<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<WebPublishMethod>MSDeploy</WebPublishMethod>
<ResourceId>/subscriptions/77e95f68-ed69-4bfe-9bbe-0b4d3910722e/resourceGroups/ResourceGroup/providers/Microsoft.Web/sites/MyWebApp</ResourceId>
<PublishProvider>AzureWebSite</PublishProvider>
<LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
<LastUsedPlatform>Any CPU</LastUsedPlatform>
<LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
<ExcludeApp_Data>False</ExcludeApp_Data>
<ProjectGuid>77e95f68-ed69-4bfe-9bbe-0b4d3910722e</ProjectGuid>
<MSDeployServiceURL>mywebapp.scm.azurewebsites.net:443</MSDeployServiceURL>
<DeployIisAppPath>MyWebApp</DeployIisAppPath>
<RemoteSitePhysicalPath />
<SkipExtraFilesOnServer>False</SkipExtraFilesOnServer>
<MSDeployPublishMethod>WMSVC</MSDeployPublishMethod>
<EnableMSDeployBackup>True</EnableMSDeployBackup>
<UserName>$MyWebApp</UserName>
<_SavePWD>True</_SavePWD>
<_DestinationType>AzureWebSite</_DestinationType>
<TargetFramework>net6.0</TargetFramework>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<SelfContained>false</SelfContained>
<InstallAspNetCoreSiteExtension>False</InstallAspNetCoreSiteExtension>
<EnableMsDeployAppOffline>True</EnableMsDeployAppOffline>
</PropertyGroup>
</Project>
(注意: ResourceId
、 ProjectGuid
、 &c. 已匿名化,不涉及实际属性。)题
承认这是新版本 Visual Studio 中的新功能,这可能是一个错误。也就是说,有没有
pubxml
启用 <ImplicitUsing />
所需的属性特征?或者是否需要通过 Visual Studio 发布配置文件启用此功能?
最佳答案
首先,这似乎是一个误报。在安装 Visual Studio 2022 的发布版本后,我显然没有完成重新启动我的工作站。重新启动后,我现在可以在完全支持隐式使用的情况下发布。哎呀。
也就是说,这确实提供了一个很好的机会,让我深入了解我事后发现的内容,这可能有助于 future 遇到类似问题的读者——当然也有助于我更好地理解各种命令行工具之间的集成。
命令行参数
值得注意的是,似乎没有任何命令行参数用于处理以下任何工具中内置的隐式使用:
dotnet.exe
) csc.exe
) msbuild.exe
) 微软构建支持
相反,这是在处理 C# 项目文件 (
msbuild.exe
) 时通过 Microsoft 构建引擎 ( *.csproj
) 处理的,此时它会在以下位置生成以下中间文件:/{BaseIntermediateOutputPath}/{Configuration}/net6.0/{MyWebApp}.GlobalUsings.g.cs
例如,默认情况下:/obj/Release/net6.0/MyWebApp.GlobalUsings.g.cs
此文件包含全局 using
特定于配置的 SDK 的指令:// <auto-generated/>
global using global::Microsoft.AspNetCore.Builder;
global using global::Microsoft.AspNetCore.Hosting;
…
反过来,这个文件被附加到对 C# 编译器的底层调用 ( csc.exe
)。Note: The .NET 6.0 SDK—i.e.,
dotnet.exe
—provides a wrapper aroundmsbuild.exe
as part of either thebuild
orpublish
commands, and thus offers the same functionality. Further, by default, the .NET SDK will persist the intermediate files in the/obj
folder, whereasmsbuild.exe
immediately deletes them.
C# 10 支持
鉴于此,隐式 using 特性实际上并不是 C# 10 本身的一部分——despite being advertised as such — 而是 Microsoft 构建引擎 (
msbuild.exe
) 中内置的一些工具。 C# 10 编译器 ( csc.exe
) 确实支持 global using
directives ,但不知道隐式使用。最终,这并不太令人惊讶,因为 C# 编译器 (
csc.exe
) 实际上并不知道 C# 项目文件 ( *.csproj
) 本身——它们是 Microsoft Build Engine 的产物,通过.NET SDK。结论
目前尚不清楚为什么本地构建在完全重启之前成功运行,而使用 Visual Studio 2022 的发布配置文件调用失败。我认为这与 Visual Studio 在发布过程中如何为 C# 编译器 (
csc.exe
) 组装参数有关。无论如何,在安装后重新启动工作站是一个简单的解决方案。否则,这篇文章将有望帮助揭开整个过程的神秘面纱。
关于.net - 通过 Visual Studio 2022 使用 C# 10 隐式使用发布 .NET 6 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69907525/