【发布时间】:2020-09-21 22:18:36
【问题描述】:
问题
我有一个使用 IIS 托管 InProcess 的 ASP.NET Core Web 应用程序。我实现了每次启动时抛出的异常。 如预期的那样,IIS 显示此错误页面:
HTTP 错误 500.30 - ANCM 进程中启动失败
此问题的常见解决方案:
- 该 应用程序启动失败
- 应用程序启动但 然后停止
- 应用程序启动但抛出异常 启动时
故障排除步骤:
- 检查系统事件 记录错误消息
- 启用记录应用程序 进程的标准输出消息
- 将调试器附加到 申请流程和检查
欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?LinkID=2028265
在 .Net Core 2.2(使用 CaptureStartupErrors(false))以及经典 ASP.NET 应用程序中,IIS 会尝试在下一个请求时再次启动应用程序。
对于 .Net Core 3.1,它不会尝试重新启动,它只会永远保持这种状态,无论我为 CaptureStartupErrors 设置什么。
解决方法
我可以通过捕获异常并退出来解决此问题 - 如果我这样做,它将按预期运行:
public static void Main(string[] args)
{
try
{
CreateWebHostBuilder(args).Build().Run();
}
catch (Exception)
{
Environment.Exit(-1);
}
}
预期行为
对于如何更改此行为的任何想法,我都会很高兴,因此在不使用此解决方法的情况下,它的行为与以前相同。如果我的应用程序抛出未处理的异常,它应该退出并尝试在下一个请求时重新启动。
我尝试过的
- 为
CaptureStartupErrors使用不同的值 - 寻找改变这种行为的配置参数
- 在“发布”-配置而不是“调试”中发布
- 创建了issue on Github
重现问题的代码
当您将应用程序发布到 IIS 时会出现此问题,并且在没有 IIS 的情况下无法重现。
程序.cs:
public class Program
{
public static void Main(string[] args)
{
File.AppendAllText("log.txt", $"{DateTime.Now.ToLongTimeString()}:Starting\r\n");
CreateWebHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateWebHostBuilder(string[] args) =>
Host.CreateDefaultBuilder( args )
.ConfigureWebHostDefaults( webBuilder =>
{
webBuilder
.UseStartup<Startup>();
} );
}
Startup.cs:
public class Startup
{
public Startup( IConfiguration configuration )
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices( IServiceCollection services )
{
}
public void Configure( IApplicationBuilder app, IHostingEnvironment env )
{
throw new Exception();
}
}
以下文件大多是默认的,但如果你想检查:
web.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="." inheritInChildApplications="false">
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="dotnet" arguments=".\WebApplication20.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="InProcess" />
</system.webServer>
</location>
</configuration>
.csproj:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
</ItemGroup>
</Project>
文件夹配置文件.pubxml:
<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<DeleteExistingFiles>False</DeleteExistingFiles>
<ExcludeApp_Data>False</ExcludeApp_Data>
<LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
<LastUsedBuildConfiguration>Debug</LastUsedBuildConfiguration>
<LastUsedPlatform>Any CPU</LastUsedPlatform>
<PublishProvider>FileSystem</PublishProvider>
<PublishUrl>C:\inetpub\wwwroot\test</PublishUrl>
<WebPublishMethod>FileSystem</WebPublishMethod>
<SiteUrlToLaunchAfterPublish />
<TargetFramework>netcoreapp3.1</TargetFramework>
<ProjectGuid>9ec27b57-5f45-4286-aa7c-12abad61a153</ProjectGuid>
<SelfContained>false</SelfContained>
</PropertyGroup>
</Project>
【问题讨论】:
-
什么错误导致应用程序崩溃,这个错误可以解决吗?
-
这更像是一个一般性问题,但我来自
SocketFailure (ReadSocketError/ConnectionReset,在连接到我们的 Redis 服务器时系统重新启动后随机发生,因为系统的某些部分似乎没有完全初始化此时。实施重试是一种选择,但我们更愿意使用 IIS 的本机功能来包装完整的应用程序并处理所有未来的事件,而不是针对这个问题的单一案例实施特定的解决方案。 -
@Compufreak 我对这个问题有类似的感觉,我什至在 github 上写了评论github.com/dotnet/aspnetcore/issues/…