异常日志记录中的空引用异常屏蔽true错误

时间:2012-11-05 15:26:57

标签: c# asp.net multithreading iis-7 mutex

我们有一个由Web请求启动的长时间运行的进程。为了完成处理时间,我们在新线程上将其分离,并使用Mutex确保只有一个进程实例可以运行。此代码在我们的开发和登台环境中按预期运行,但在我们的生产环境中使用Null Reference Exception失败。我们的应用程序日志记录不捕获任何内容,我们的操作人员报告它正在崩溃AppPool。 (这似乎是一个环境问题,但我们必须继续假设环境配置相同。)到目前为止,我们无法确定空引用的位置。

以下是应用程序事件日志的例外:

Exception: System.NullReferenceException
Message: Object reference not set to an instance of an object.
StackTrace:    at Jobs.LongRunningJob.DoWork()
   at System.Threading.ExecutionContext.runTryCode(Object userData)
   at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

这是代码(略有消毒):

public class LongRunningJob: Job
{
    private static Mutex mutex = new Mutex();

    protected override void PerformRunJob()
    {
        var ts = new ThreadStart(LongRunningJob.DoWork);
        var thd = new Thread(ts);
        thd.IsBackground = true;
        thd.Start();
    }

    private static void DoWork()
    {
        var commandTimeOut = 180;

        var from = DateTime.Now.AddHours(-24);
        var to = DateTime.Now;

        if (mutex.WaitOne(TimeSpan.Zero))
        {
            try
            {
               DoSomethingExternal(); // from what we can tell, this is never called
            }
            catch (SqlException sqlEx)
            {
                if (sqlEx.InnerException.Message.Contains("timeout period elapsed"))
                {
                    Logging.LogException(String.Format("Command timeout in LongRunningJob: CommandTimeout: {0}", commandTimeOut), sqlEx);
                }
                else
                {
                    Logging.LogException(String.Format("SQL exception in LongRunningJob: {0}", sqlEx.InnerException.Message), sqlEx);
                }
            }
            catch (Exception ex)
            {
                Logging.LogException(String.Format("Error processing data in LongRunningJob: {0}", ex.InnerException.Message), ex);
            }
            finally
            {
                mutex.ReleaseMutex();
            }
        }
        else
        {
            Logging.LogMessage("LongRunningJob is already running.");
        }
    }
}

1 个答案:

答案 0 :(得分:3)

为了找到NullReferenceException,您基本上会检查每个解除引用操作。我只能看到以下可疑的一个:

ex.InnerException.Message

您不能认为ex.InnerException不为空。

相关问题