question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

IHostApplicationLifetime ApplicationStopping is not waiting on registrations

See original GitHub issue

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

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
FrenkRcommented, Apr 5, 2020

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 😃

1reaction
FrenkRcommented, Jan 8, 2020

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What the point to have IHostApplicationLifetime when you ...
According to the documentation ApplicationStarted is triggered when the 'application host if fully started'. This 'event' could then be used by ...
Read more >
Waiting for your ASP.NET Core app to be ready from an ...
In this post I describe how you can wait for your ASP.NET Core app to be ready to receive requests from inside an...
Read more >
Application Shutdown in ASP.NET Core with ...
I use ASP.NET Core 3.1 and it works just fine. In the above sample, simply change “IApplicationLifetime” to “IHostApplicationLifetime” and your ...
Read more >
NET Generic Host
After the host has been started, when a user calls Run or WaitForShutdown , a handler gets registered for IApplicationLifetime.
Read more >
Graceful termination in Kubernetes with ASP.NET Core
NET Core is not willing to wait infinitely for the pending ... Register(() => { Log("ApplicationStopping called, sleeping for 10s"); Thread.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found