如何使应用程序在启动时失败

DaI*_*mTo 1 c# .net-core asp.net-core

我有一个 .net core 应用程序。如果当前配置不正确,我需要它失败它会继续运行,我需要它关闭。

在此输入图像描述

我的配置服务看起来像这样。必须有一种方法可以让应用程序在配置错误时停止。

public void ConfigureServices(IServiceCollection services)
    {

        if (Configuration["settings:connectionString"] == null) return;

   }
Run Code Online (Sandbox Code Playgroud)

抛出异常

不会停止应用程序,它所做的只是将其保留在我的日志中

应用程序启动异常:System.ArgumentNullException:值不能为空。参数名称:settings:connectionString 在 C:\Development\Xena\xena-identityserver\src\Xena.IdentityServer\Startup.cs 中的 Xena.IdentityServer.Startup.ConfigureServices(IServiceCollection services) 处未正确定义:第 89 行 --- 结束来自先前抛出异常的位置的堆栈跟踪 --- 在 Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection services) 在 Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices 的 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication() 处的 () crit:Microsoft.AspNetCore.Hosting.Internal.WebHost[6] 应用程序启动异常 System.ArgumentNullException:值不能为 null。参数名称:settings:connectionString 在 C:\Development\Xena\xena-identityserver\src\Xena.IdentityServer\Startup.cs 中的 Xena.IdentityServer.Startup.ConfigureServices(IServiceCollection services) 处未正确定义:第 89 行 --- 结束来自先前抛出异常的位置的堆栈跟踪 --- 在 Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection services) 在 Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices 的 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() () 在 Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()

该网站仍然开放,并且可以看到上面的相同页面。我需要停止该应用程序。

小智 5

    public void ConfigureServices(IServiceCollection services)
    {

        if (Configuration["settings:connectionString"] == null)
        {
             // Log?
             System.Environment.Exit(160);
        }

   }
Run Code Online (Sandbox Code Playgroud)

自从微软惯例建议将此用于“糟糕的论点”以来,我就一直使用160这里,但我愿意接受这方面的建议。

https://learn.microsoft.com/en-gb/windows/desktop/Debug/system-error-codes--0-499-