.net-5 - 在 ASP.NET Core 5 Web 应用程序中禁用 DOTNET_STARTUP_HOOKS
问题描述
将我的 ASP.NET Core 5 应用程序(以 .NET 5 为目标)部署到 Azure 应用程序服务时,它会引发 502 错误,并记录以下详细信息:
<Event>
<System>
<Provider Name=".NET Runtime"/>
<EventID>1026</EventID>
<Level>1</Level>
<Task>0</Task>
<Keywords>Keywords</Keywords>
<TimeCreated SystemTime="2021-07-12T08:58:41Z"/>
<EventRecordID>-2033762671</EventRecordID>
<Channel>Application</Channel>
<Computer>RD0003FF7ABC48</Computer>
<Security/>
</System>
<EventData>
<Data>Application: dotnet.exe
CoreCLR Version: 5.0.721.25508
.NET Version: 5.0.7
Description: The process was terminated due to an unhandled exception.
Exception Info: System.ArgumentException: Startup hook assembly 'C:\Program Files\dotnet\SDK\5.0.301\DotnetTools\dotnet-watch\5.0.301-servicing.21271.7\tools\net5.0\any\middleware\Microsoft.AspNetCore.Watch.BrowserRefresh.dll' failed to load. See inner exception for details.
---> System.IO.FileNotFoundException: Could not load file or assembly 'C:\Program Files\dotnet\SDK\5.0.301\DotnetTools\dotnet-watch\5.0.301-servicing.21271.7\tools\net5.0\any\middleware\Microsoft.AspNetCore.Watch.BrowserRefresh.dll'. The system cannot find the path specified.
File name: 'C:\Program Files\dotnet\SDK\5.0.301\DotnetTools\dotnet-watch\5.0.301-servicing.21271.7\tools\net5.0\any\middleware\Microsoft.AspNetCore.Watch.BrowserRefresh.dll'
at System.Runtime.Loader.AssemblyLoadContext.LoadFromPath(IntPtr ptrNativeAssemblyLoadContext, String ilPath, String niPath, ObjectHandleOnStack retAssembly)
at System.Runtime.Loader.AssemblyLoadContext.LoadFromAssemblyPath(String assemblyPath)
at System.StartupHookProvider.CallStartupHook(StartupHookNameOrPath startupHook)
--- End of inner exception stack trace ---
at System.StartupHookProvider.CallStartupHook(StartupHookNameOrPath startupHook)
at System.StartupHookProvider.ProcessStartupHooks()
</Data>
</EventData>
</Event>
未找到的路径对应于在 web.config 中设置的环境变量,该环境变量在我构建应用程序时由 VisualStudio 自动设置:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<!--
Configure your application settings in appsettings.json. Learn more at http://go.microsoft.com/fwlink/?LinkId=786380
-->
<!--App settings needed to the Cloud media service library-->
<system.web>
<customErrors mode="off" />
</system.web>
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false" startupTimeLimit="3600" requestTimeout="23:00:00">
<environmentVariables>
<environmentVariable name="COMPLUS_ForceENC" value="1" />
<environmentVariable name="ASPNETCORE_HTTPS_PORT" value="8088" />
<environmentVariable name="ASPNETCORE_AUTO_RELOAD_WS_ENDPOINT" value="ws://localhost:53593/MyWebApp/" />
<environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
<environmentVariable name="ASPNETCORE_HOSTINGSTARTUPASSEMBLIES" value="Microsoft.AspNetCore.Watch.BrowserRefresh" />
<environmentVariable name="DOTNET_STARTUP_HOOKS" value="**C:\Program Files\dotnet\SDK\5.0.301\DotnetTools\dotnet-watch\5.0.301-servicing.21271.7\tools\net5.0\any\middleware\Microsoft.AspNetCore.Watch.BrowserRefresh.dll**" />
</environmentVariables>
</aspNetCore>
</system.webServer>
</configuration>
这个环境变量是做什么用的?我可以禁用它的生成以允许应用在部署时在 Azure 应用服务上正常运行吗?
提前致谢。
解决方案
可能不是最花哨的解决方案,而是手动删除DOTNET_STARTUP_HOOKS
环境变量中引用的DLL,web.config
防止VS自动设置一遍又一遍。
推荐阅读
- c - 存储 fftwf_plans 数组?
- c# - 在通用 HTTPClient 类中添加错误处理
- sql - MAX函数在一列上,返回所有列
- python - MacOS Mojave 上的“没有名为 Django 的模块”问题
- java - Java:字符与字符串的 HashSet 行为
- ruby-on-rails - Rails 双向accepts_nested_attributes_for 导致双重保存和脏属性丢失
- php - 未找到 XAMPP 对象
- python - 你能简化 for 循环吗?
- excel - 循环遍历特定的工作表并返回特定的值
- python - wxpython 错误,Gtk 消息:无法加载模块“atk-bridge”