When an application pool and its associated worker processes are being shut down by IIS, for example when an application pool has reached a scheduled recycle interval, IIS notifies the ASP.NET runtime that the application is shutting down and the worker process for the application pool stops listening for new requests. Applications that use long-running requests, for example SignalR applications, can listen for the StopListening event and gracefully shut down their long-running operations when IIS has initiated the shutdown of an application pool and its associated worker processes.
There are several design considerations that a developer must consider when implementing the StopListening event:
The StopListening event is only available for applications that are configured to use the IIS integrated pipeline.
Developers must not throw errors from their StopListening implementations; throwing errors will have unpredictable results.
Due to the concurrent nature of operations within ASP.NET, there is no guarantee that ASP.NET will call a StopListening event once it has been implemented by a developer.
The StopListening event can execute concurrently with other IRegisteredObject notifications.
Developers need to be aware that ASP.NET may shut down the application domain while their StopListening event is executing and design their implementations to handle that possibility.
StopListening event handlers must not block; blocking implementations will result in deadlocks, and IIS will terminate the worker process when the rapid fail protection interval is reached.