在我的Windows服务代码中,我有一个try catch块。 当我在数据库上的开发机器上运行单元测试时,单元测试通过。
当服务在具有相同数据库的生产服务器上运行时,会发生错误,并且Windows事件日志中会出现两个事件。
EventType clr20r3, P1 MKG.XPREFLIGHT.servicehost.exe, P2 2.0.62.0,
P3 574a1457, P4 mscorlib, P5 4.0.0.0, P6 53b50a71, P7 3e5, P8 10d,
P9 system.formatexception, P10 NIL.
和
Event Type: Error
Event Source: .NET Runtime
Event Category: None
Event ID: 1026
Date: 29/05/2016
Time: 8:03:54 AM
User: N/A
Computer: SERVERX1
Description:
Application: MKG.XPREFLIGHT.ServiceHost.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.Reflection.TargetInvocationException
Stack:
at System.RuntimeMethodHandle._InvokeMethodFast(System.IRuntimeMethodInfo, System.Object, System.Object[], System.SignatureStruct ByRef, System.Reflection.MethodAttributes, System.RuntimeType)
at System.RuntimeMethodHandle.InvokeMethodFast(System.IRuntimeMethodInfo, System.Object, System.Object[], System.Signature, System.Reflection.MethodAttributes, System.RuntimeType)
at System.Reflection.RuntimeMethodInfo.Invoke(System.Object, System.Reflection.BindingFlags, System.Reflection.Binder, System.Object[], System.Globalization.CultureInfo, Boolean)
at System.Reflection.RuntimeMethodInfo.Invoke(System.Object, System.Reflection.BindingFlags, System.Reflection.Binder, System.Object[], System.Globalization.CultureInfo)
at System.Reflection.MethodBase.Invoke(System.Object, System.Object[])
at MKG.XPREFLIGHT.Engine.FlightEngine.ParseCommand(MKG.ServiceData.EngineCommandQueue)
at MKG.XPREFLIGHT.Engine.FlightEngine.TakeCommands()
at MKG.XPREFLIGHT.Engine.FlightEngine.PollingProc(System.Object)
at System.Threading._TimerCallback.TimerCallback_Context(System.Object)
at System.Threading.ExecutionContext.runTryCode(System.Object)
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode, CleanupCode, System.Object)
at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading._TimerCallback.PerformTimerCallback(System.Object)
我隔离了发生错误的代码区域,但由于单元测试通过,我无法理解错误发生的原因。
错误是可重复的,尽管在服务器重启后第一次进程运行时,故障不会发生在同一个地方。
代码在Try Catch块中,但它不会被捕获。相反,服务在Windows事件日志中停止并出现错误。
我该如何解决这个问题? [更新]
失败的代码是
public Job GetDeepJobWithUploadInfo(int jobId)
{
// if I throw an error here it is caught
Job job = null;
using (var connect = MakeConnect())
{
job =
connect.job.AsNoTracking()
.Include(
j =>
j.VivUpload.Select(
u =>
u.VivSplitUpload.Select(
s =>
s.VivSplitProfile.Select(
p => p.VivSplitLog.Select(l => l.VivSplitLogType)))))
.Include(j => j.VivUpload.Select(u => u.VivSplitUpload.Select(s => s.VivSplitProfile)))
.Include(j => j.JobTag)
.SingleOrDefault(j => j.JobID == jobId);
}
// never gets to here in production, but does get here in unit test
return job;
}
我尝试在edmx上运行自定义工具来重新生成Entity Framework类。