extensions: IHostApplicationLifetime ApplicationStopping is not waiting on registrations

From @FrenkR on Saturday, January 4, 2020 10:19:02 AM

IHostApplicationLifetime ApplicationStopping is not waiting events

when registering IHostApplicationLifetime.ApplicationStopping event, event is called, but application stops before registered event is finished. According to documentation, application should wait for events (to be fully executed).

Steps to reproduce

1.) create new “asp.net core web application” using standard VS template.

2.) inject “IHostApplicationLifetime” into “configure” method and register “applicationStopping” event:

`

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime hostApplicationLifetime )
    {
        app.UseRouting();
        app.UseAuthorization();
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
        hostApplicationLifetime.ApplicationStopping.Register(() =>
        {
            var _i = 0; // place debug breakpoint here!
            _i++;
        });

    }

`

3.) start application in debug mode using “console” start. After starting, console window appears. 4.) set breakpoint inside “register” method. Then click “close”(“x” on right upper corner of console application). You will notice that “applicationstopping” event is called, this is correct. But if you just wait on a breakpoint for 5-10 secons, you will notice that application has stopped without waiting for event to be executed. `

Copied from original issue: dotnet/core#4072

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 1
  • Comments: 31 (13 by maintainers)

Most upvoted comments

hehe, this can also be an explanation. But years ago we called something like that “a bug” and usually it was fixed too. Thank you for your assistance and explanation 😃

LifetimeBug.zip Sample is attached. ShutdownTimeout is set to 30 seconds, inside is event that lasts 25 seconds (thread.sleep()), but application still finishes in about 5 seconds.