none
Howto: Detect Application Pool Recycle Request inside WCF Service RRS feed

  • Question

  • Hello,

    i am developing a WCF Service hosted in Sharepoint 2010.

    Is there a possibility to detect the occurrence of an Application Pool Recycle Request programmatically?

    I already tried to attach of the Closing event of the Service Host, but the problem is: if there are background worker threads active, the Service Host won't close. That's why i want to detect the occurrence of the Request, and notify the worker threads to quit working.

    Haven't found anything else useful.

    Thanks a lot,

    Joachim

    Thursday, September 19, 2013 7:03 AM

All replies

  • Hi Joachim,

    For WCF itself, it cannot directly detect whether the hosting application pool has been recycled. If you host it in ASP.NET web application in IIS, you can consider using the ASP.NET's Application_End event (regisger in Global.asax) to detect that application has ended (restarted).

    #Logging ASP.NET Application Shutdown Events
    http://weblogs.asp.net/scottgu/archive/2005/12/14/433194.aspx

    Or another possible approach is to register Unload event on the current AppDomain of the hosting ASP.NET web application. When process recycle, the .NET AppDomain should also be unloaded.

    #AppDomain.DomainUnload Event
    http://msdn.microsoft.com/en-us/library/system.appdomain.domainunload.aspx


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Friday, September 20, 2013 3:09 AM
    Moderator
  • Hello Steven,

    thanks for the quick reply. As i mentioned, i do not want to react to "when the application pool has been recycled", but rather to "when an application pool recycle has been requested".

    I tried out on the AppDomain approach, here is my findings:

    The AppDomain.Unload occurs after the ServiceHost.Closing, therefore it also waits for the Service Process to end. If this takes too long, a second process is started inside the App Pool before the current process has ended.

    For Windows Services, You can attach to Service Control Manager messages with RegisterServiceCtrlHandler, and i was wondering if there is a similar possibility for WCF Services.

    Kind regards,

    Joachim

    Friday, September 20, 2013 6:57 AM