How to detect if web site has been stopped?


G

Guest

I have an ASP.NET application that runs threads in background. Application
does not close if the web site is stopped. So how can the application get an
event when the site has been stopped, so that it could shutdown itself?

Application already closes itself gracefully if Application_Disposed is
called, but this is not called in this case.

Thanks, Harri
 
Ad

Advertisements

M

Mark S. Milley, MCAD (BinarySwitch)

Are you stopping the application from IIS, or are you expecting the
application to stop when you hit the stop button on the browser?

The web client's stop button mearly stops the current http transfer at
the client level (closing the ports). Stopping the web browser will
have no effect on the application, which shuts down only after all
sessions have ended, so the actual time that the session ends will vary
based on a variety of factors outside of your control.

If a client requests a page--any page--it will begin a session for that
user. If the user does not request another page, the session will end
after 20 minutes (by default-you can change the session timeout). If
another page is requested before the session times out, the session
will end 20 minutes after the last page request. Only after all
sessions have timed out will the application close (and even then,
you'll probably have to wait another minute or so until the next
garbage collection for it to really leave memory).

-Mark
 
G

Guest

OK, but if the whole web site is stopped in IIS, is there any way to find out
that?

What is the .NET object that represents the web site, where the current
ASP.NET application is running?
 
D

Damien

Harri said:
OK, but if the whole web site is stopped in IIS, is there any way to find out
that?

What is the .NET object that represents the web site, where the current
ASP.NET application is running?

Having long-running threads within the ASP.NET application is not
recommended. If it's not too late to re-architect, could you not have
your threads in a separate process and use remoting to communicate with
them? That way, I believe, the Application_End event will fire and you
can tell the other process to shut down.

If you must keep the threads within the application, I think you'll be
looking at kludges. Most horrendous one I can think of is to
periodically perform a web request against yourself - if you don't
respond, there's a good chance you've been shut down :-D

Sorry I can't think of something more constructive here, it's not an
exact match for situations I've had in the past. I've had long-running
threads, but not even had to consider the site being stopped whilst
they're working.

Damien
 
Ad

Advertisements

H

Harri Pesonen

Damien said:
Having long-running threads within the ASP.NET application is not
recommended. If it's not too late to re-architect, could you not have
your threads in a separate process and use remoting to communicate with
them? That way, I believe, the Application_End event will fire and you
can tell the other process to shut down.

If you must keep the threads within the application, I think you'll be
looking at kludges. Most horrendous one I can think of is to
periodically perform a web request against yourself - if you don't
respond, there's a good chance you've been shut down :-D

Sorry I can't think of something more constructive here, it's not an
exact match for situations I've had in the past. I've had long-running
threads, but not even had to consider the site being stopped whilst
they're working.

OK, thanks. I already have Application_Disposed implemented and it works
fine, application closes gracefully, if for example web.config is touched.

Application_End didn't work as well, it was not called. Btw, why I need
to add Application_Disposed event manually, while
Application_BeginRequest is called automatically? I added the following
code:

public Global() {
InitializeComponent();
this.Disposed += new EventHandler(Application_Disposed);
}

After this Application_Disposed is called properly. What is the
mechanism in C# so that Application_BeginRequest is called automatically?

It just would make sense to react to web site stopping as well, as this
seems to be the only GUI method for stopping a web application.

-- Harri
 

Top