How to tell why an IIS application pool is recycled

后端 未结 4 1147
走了就别回头了
走了就别回头了 2020-12-25 14:31

Background:

I\'ve deployed an ASP.NET MVC 3 app that works on my machine to a shared hosting provider and am discovering some problems that appear to be related to

4条回答
  •  隐瞒了意图╮
    2020-12-25 15:08

    Without access to the event logs (because you're in a shared hosting environment) the most information you're going to get is from the Application_End event and by asking the HttpRuntime (via reflection) for the values of one or two private members that are sadly not exposed publicly.

    To do this add the following code to your Application_End event:

    BindingFlags staticFlags = 
        BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.GetField;
    BindingFlags instanceFlags = 
        BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField;
    
    HttpRuntime runtime = (HttpRuntime)typeof(System.Web.HttpRuntime)
                            .InvokeMember("_theRuntime", staticFlags, null, null, null);
    if(runtime != null) 
    {
        string shutDownMessage = (string)runtime.GetType()
             .InvokeMember("_shutDownMessage", instanceFlags, null, runtime, null);
    
        string shutDownStack = (string)runtime.GetType()
             .InvokeMember("_shutDownStack", instanceFlags, null, runtime, null);
    
        // Log shutDownMessage & shutDownStack somewhere
    }
    

    If I shutdown or recycle my app's application pool I see the following:

    HostingEnvironment initiated shutdown
    HostingEnvironment caused shutdown -    
       at System.Environment.GetStackTrace(Exception e, Boolean needFileInfo)
       at System.Environment.get_StackTrace()
       at System.Web.Hosting.HostingEnvironment.InitiateShutdownInternal()
       at System.Web.Hosting.HostingEnvironment.InitiateShutdownWithoutDemand()
       at System.Web.Hosting.PipelineRuntime.StopProcessing()
    

    That's probably about as good as it gets.

    Update:

    I couldn't remember where I found this code but Drew helpfully reminded me it was from a Scott Guthrie blog post.

    There are some other private members that could be useful such as:

    private ApplicationShutdownReason _shutdownReason;
    

    You can examine these fields in .NET Reflector (if you still have a copy that isn't time-bombed) or one of the alternatives (Open Source Alternatives to Reflector?).

提交回复
热议问题