Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DefineCriticalErrorAction access to IServiceProvider #7039

Open
dnv-kimbell opened this issue May 24, 2024 · 3 comments
Open

DefineCriticalErrorAction access to IServiceProvider #7039

dnv-kimbell opened this issue May 24, 2024 · 3 comments

Comments

@dnv-kimbell
Copy link

Describe the feature.

We are implementing DefineCriticalErrorAction() to shut down the application. In your documentation there is a mention of IHostApplicationLifetime.Stop, but the challenge is that ICriticalErrorContext doesn't give us access to an IServiceProvider where we can get access to IHostApplicationLifetime.

Our workaround is to have a hosted services that polls a static property where DefineCriticalErrorAction() can set the ICriticalErrorContext.
Not pretty.

Additional Context

No response

@bording
Copy link
Member

bording commented May 24, 2024

@dnv-kimbell

It is already possible to use IHostApplicationLifetime methods from your critical error action. For example, here's one way you can do that:

var builder = Host.CreateApplicationBuilder(args);

IHostApplicationLifetime lifetime = null;

var endpointConfiguration = new EndpointConfiguration("example");
var routing = endpointConfiguration.UseTransport(new LearningTransport());
endpointConfiguration.UseSerialization<SystemJsonSerializer>();
endpointConfiguration.DefineCriticalErrorAction((context, cancellationToken) => OnCriticalError(context, cancellationToken, lifetime));

builder.UseNServiceBus(endpointConfiguration);

var app = builder.Build();
lifetime = app.Services.GetRequiredService<IHostApplicationLifetime>();
app.Run();

static Task OnCriticalError(ICriticalErrorContext context, CancellationToken cancellationToken, IHostApplicationLifetime lifetime)
{
    lifetime.StopApplication();

    return Task.CompletedTask;
}

@dnv-kimbell
Copy link
Author

We have an application portfolio spanning 150 repos containing deployable code; mixture of ASP.NET and Windows Services. In order to keep this maintainable and consistent, we have created a highly opinionated framework on top of ASP.NET. This sets up authentication, logging, metrics, and a bunch of other things. When using external libraries such as NServiceBus, Quartz or GraphQL, we wrap them in a system that allows us to easily set them up with the rest of the system.

Your example takes advantage of C# closures and the fact that everything is in the same file. This is not something our applications can take advantage.

@johnsimons
Copy link
Member

Hi @dnv-kimbell

Thanks for providing more context to this feature request.
We acknowledge that, currently, the API is not ideal for your scenario.
We will take this into consideration in an upcoming enhancement release.

Regards
John

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants