我们有一个由Web请求启动的长期运行过程。为了使过程有时间完成,我们将其旋转在新线程上,并使用互斥X确保该过程的一个实例可以运行。该代码按照我们的开发和分期环境的目的运行,但在我们的生产环境中失败了,没有参考异常。我们的应用程序记录没有捕获任何内容,我们的操作人员报告说,它正在崩溃。(这似乎是一个环境问题,但我们必须继续假设环境是相同配置的。)到目前为止,我们无法确定零参考的位置。
这是应用程序事件日志的例外:
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.");
}
}
}
为了找到NullReferenceException
,您基本上检查了每个解雇操作。我只能看到以下可疑一个:
ex.InnerException.Message
您不能假设ex.InnerException
不是null。