1

Do class finalizers execute when IIS pool recycled, or, for that matter during pool STOP. Theoretically, this should be similar to Application.Run/Stop but is it?

In the same context, I believe, if the app is NET CORE, and it runs console app in the Kestrel or IIS, the console app shutting would run the finalizers. Or is it?

T.S.
  • 18,195
  • 11
  • 58
  • 78
  • 2
    Finalizers run when something is garbage collected. Even if you garbage collected before process termination (which isn't normal), that doesn't mean everything is collected. What are you trying to do? If you're trying to detect shutdown to do necessary processing, then: don't do that via finalizers. In fact, finalizers are *incredibly* rare, and you probably shouldn't be using them. – Marc Gravell Aug 14 '20 at 17:53
  • @MarcGravell I remember, if you close windows app normally, finalizers run. Looks like there is last GS running during that time. Yes. So, basically, need to release some remote resources . – T.S. Aug 14 '20 at 18:11
  • Then IMO you are asking the wrong question; the real question is "how to detect asp.net shutdown" (in the context of IIS and Kestrel separately, perhaps). I don't have a PC in front of me right now, by we do this *all the time* – Marc Gravell Aug 14 '20 at 18:54
  • @MarcGravell perhaps.. but this is still an interesting question, in my opinion. Something to know. – T.S. Aug 14 '20 at 19:00

1 Answers1

0

To detect Application Shutdown in ASP.NET Core you could add an event in the Configure method.

Below is the list of the strtup class:

1)IApplicationBuilder

2)IHostingEnvironment

3)ILoggerFactory

4)IApplicationLifetime

public class Startup
{
    public void Configure(IApplicationBuilder app, IApplicationLifetime applicationLifetime)
    {
        applicationLifetime.ApplicationStopping.Register(OnShutdown);
    }
 
    private void OnShutdown()
    {
         //this code is called when the application stops
    }
}

https://gist.github.com/davidfowl/4015810

Jalpa Panchal
  • 8,251
  • 1
  • 11
  • 26
  • Thanks for your input. This is interesting, however, your answer does not directly answers the question and in this case it is only applicable to things "core" and not, for example, to old style asp.net WSPs. The question is, again, mostly, if there is a mechanism during IIS app pool shutdown that some IIS component/handler calls GC to collect and finalizers executed. And if some happen to have code in them, this code executes before pool finally shuts down. – T.S. Aug 18 '20 at 14:52
  • I would say, at this point this is more of an academic interest – T.S. Aug 18 '20 at 14:53
  • @T.S. Os helps to free up the memory, not GC you can refer this link:https://stackoverflow.com/questions/31793305/does-process-termination-automatically-free-all-memory-used-any-reason-to-do-it – Jalpa Panchal Aug 25 '20 at 09:47
  • This is close related to what I am taking about.Not what you're talking about https://stackoverflow.com/questions/15246167/does-garbage-collection-happen-at-the-process-level-or-appdomain-level – T.S. Aug 25 '20 at 13:10