项目中遇到的问题

  排查原因

  近调试一个东西,写了个监控页面,发现自己Cache的数据在设定的过期时间远远还没有到的时候过期了,于是我着手排查了下出现问题的原因:

  1、程序会删除缓存 -- 查找了整个项目,发现程序里根本没有删除缓存的操作,所以这个原因可以排除。

  2、写缓存的方法有问题 -- 这个也可以排除,因为很多地方用这个dll,其它项目不存在这个bug,所以这个原因也可以排除。

  3、服务器上有清除所有内存的程序 -- 于是一个个服务终止了下,后发现终止了一个动转静服务后,程序没有异常了,所以这个问题是因为动转静服务引起的了。

  解决方案

  反编译了这个动转静服务的源码,发现并没有清理内存的操作,于是想到了是不是因为动转静服务的什么操作,引起了应用程序池Appliacation_End,然后cache都被回收了。于是在Global.asax里面的Application_End方法里写了下面的代码,用于记录是否引起了这个事件,以及引起事件触发的原因。

protected void Application_End(object sender, EventArgs e)
{
    HttpRuntime runtime = (HttpRuntime)typeof(System.Web.HttpRuntime).InvokeMember("_theRuntime",
        BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.GetField,
        null,
        null,
        null);
 
    if (runtime == null)
        return;
 
    string shutDownMessage = (string)runtime.GetType().InvokeMember("_shutDownMessage",
        BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField,
        null,
        runtime,
        null);
 
    string shutDownStack = (string)runtime.GetType().InvokeMember(
        "_shutDownStack",
        BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField,
        null,
        runtime,
        null);
 
    if (!EventLog.SourceExists(".NETRuntime"))
    {
 
        EventLog.CreateEventSource(".NETRuntime", "Application");
 
    }
 
    EventLog log = new EventLog();
 
    log.Source = ".NET Runtime";
 
    log.WriteEntry(String.Format(" _shutDownMessage={0} _shutDownStack={1}", shutDownMessage, shutDownStack), EventLogEntryType.Error);
}

  结果,日志文件里头果然有记录,记录都为:

  =====================================================

  The description for Event ID ( 0 ) in Source ( .NET Runtime ) cannot be found. The local computer may not have the necessary registry information or message DLL files to display messages from a remote computer. You may be able to use the /AUXSOURCE= flag to retrieve this description; see Help and Support for details. The following information is part of the event:

  _shutDownMessage=Recompilation limit of 15 reached
  HostingEnvironment initiated shutdown
  HostingEnvironment caused shutdown

  _shutDownStack=   at System.Environment.GetStackTrace(Exception e, Boolean needFileInfo)
     at System.Environment.get_StackTrace()
     at System.Web.Hosting.HostingEnvironment.InitiateShutdownInternal()
     at System.Web.Hosting.HostingEnvironment.InitiateShutdown()
     at System.Web.HttpRuntime.ShutdownAppDomain(String stackTrace)
     at System.Web.Compilation.DiskBuildResultCache.ShutdownCallBack(Object state)
     at System.Threading._ThreadPoolWaitCallback.WaitCallback_Context(Object state)
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
     at System.Threading._ThreadPoolWaitCallback.PerformWaitCallbackInternal(_ThreadPoolWaitCallback tpWaitCallBack)
     at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback(Object state).

  =====================================================