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

Implement IApplicationLifetime to detect application shutdown and gracefully close connections #18

Open
timbellomo opened this issue Dec 22, 2022 · 1 comment
Labels
enhancement New feature or request

Comments

@timbellomo
Copy link
Collaborator

Some thoughts on graceful shutdown, either for app pool recycle or server reboot. Event can fire to notify clients.

public void ConfigureServices(IServiceCollection services)
{
    services.AddSignalR();
    services.AddSingleton<IApplicationLifetime>(provider =>
    {
        var lifetime = provider.GetService<IHostApplicationLifetime>();
        return lifetime;
    });
}

then

 public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IApplicationLifetime lifetime)
{
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapHub<DashboardHub>("/dashboardHub", options =>
        {
            options.AddHubOptions<DashboardHub>(o =>
            {
                o.AddClient lifetime);
            });
        });
    });
}

then

public class DashboardHub: Hub
{
    private readonly IApplicationLifetime _lifetime;
    private static CAD _cad;

    public DashboardHub(IApplicationLifetime lifetime, CAD cad)
    {
        _lifetime = lifetime;
        _lifetime.ApplicationStopping.Register(OnStopping);
        _cad = cad;
    }

    private void OnStopping()
    {
        // Code to handle the shutdown goes here
    }
}

There may be better ways to handle this.

@timbellomo timbellomo added the enhancement New feature or request label Dec 22, 2022
@timbellomo
Copy link
Collaborator Author

Actually - IApplicationLifetype is obsolete; use IHostApplicationLifetime instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

When branches are created from issues, their pull requests are automatically linked.

1 participant