【问题标题】:Disable DOTNET_STARTUP_HOOKS in ASP.NET Core 5 web app在 ASP.NET Core 5 Web 应用程序中禁用 DOTNET_STARTUP_HOOKS
【发布时间】:2024-05-23 08:20:02
【问题描述】:

将我的 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 应用服务上正常运行?

提前致谢。

【问题讨论】:

  • 不需要的可以手动从web.config中删除,下次创建应用时不要签入
  • 是的,我可以从 web.config 中手动删除它,但如果我这样做了,VS 会重新创建它。
  • 这就是我的意思,所以不要再次签入 web.config 文件。因此在部署期间不会考虑您的新更改
  • 感谢您的帮助。你能更准确一点吗?不签入 web.config 是什么意思?如果您打算通过 .gitignore 或类似方式将其排除在外,我认为这不是一个好习惯,因为将来对此文件所做的新的重要更改也可以忽略。我认为如果有任何选项/语句可以禁用此环境变量的创建,那就太好了。

标签: .net-5 azure-appservice asp.net5


【解决方案1】:

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

【讨论】: