首页 > 解决方案 > 在 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.
---&gt; 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 应用服务上正常运行吗?

提前致谢。

标签: .net-5azure-appserviceasp.net5

解决方案


可能不是最花哨的解决方案,而是手动删除DOTNET_STARTUP_HOOKS环境变量中引用的DLL,web.config防止VS自动设置一遍又一遍。


推荐阅读