。NET Core UseExceptionHandler DI

问题描述 投票:2回答:1

我在Startup.ConfigureServices期间将自定义错误日志记录和报告服务注入到我的服务集合中。

services.AddTransient<IErrorService, ErrorService>();

然后我在Startup.Configure中添加一个全局错误陷阱。

app.UseExceptionHandler("/Home/Error");

然后在Home.Error中,我有用于ErrorService的DI,我将其传递给上下文,并且一切正常。

现在,我想知道是否有某种方式可以消除与Home.Error的链接,而只在Configure中调用我的ErrorService?但是我现在还无法在Startup中弄清楚如何链接到ErrorService。注意,我注入的其他两个服务是我的EmailService和我的ProgramSettings。我在ErrorService中都需要它们,因此我确实需要以连接标准.NET Core DI的方式调用ErrorService。

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        app.UseExceptionHandler(a => {
            a.Run(ctx => {
                var exceptionFeature = ctx.Features.Get<IExceptionHandlerPathFeature>();

                     ... execute ErrorService here somehow????????

                ctx.Response.StatusCode = StatusCodes.Status500InternalServerError;
                return Task.CompletedTask;
            });
        });
.net startup core custom-error-handling
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.