我有这个类型的例外System.AggregateException
:
Message = "One or more errors occurred."
Source = null
StackTrace = null
它InnerException
是这样的System.Reflection.TargetInvocationException
:
Message = "Exception has been thrown by the target of an invocation."
Source = "mscorlib"
StackTrace =
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
at Microsoft.AspNet.SignalR.Hubs.HubDispatcher.Incoming(IHubIncomingInvokerContext context)
它InnerException
是我创建的这个例外,它从以下延伸ApplicationException
:
Message = "Some error writen by me at the hub"
Source = "Chat"
StackTrace =
at Chat.Hubs.ChatHub.SendMessage(String text, String clientConnId) in d:\...Chat\Chat\Hubs\ChatHub.cs:line 48
当我运行这个:
Exception excpetion = ex.GetBaseException();
我在哪里ex
得到at 。这怎么可能发生?System.AggregateException
System.Reflection.TargetInvocationException
excpetion
设想
我不知道如何在一个简单的项目中重现它。就我而言,我在 SignalR 项目中发现了这一点。一些集线器方法抛出异常并在 global.asax 处理错误:
GlobalHost.HubPipeline.AddModule(new MyHubPipelineModule());
MyHubPipelineModule
应该是这样的:
public class MyHubPipelineModule : HubPipelineModule
{
protected override void OnIncomingError(Exception ex, IHubIncomingInvokerContext context)
{
Exception excpetion = ex.GetBaseException();
context.Hub.Clients.Caller.ExceptionHandler(excpetion.Message);
}
}
注意:这应该使用 SignalR 1.0.1 完成。在 1.1.0 中异常更简单(更小的链)所以它运行良好。确保你有这个包版本:
<package id="Microsoft.AspNet.SignalR" version="1.0.1" targetFramework="net40" />
<package id="Microsoft.AspNet.SignalR.Core" version="1.0.1" targetFramework="net40" />
<package id="Microsoft.AspNet.SignalR.JS" version="1.0.1" targetFramework="net40" />
<package id="Microsoft.AspNet.SignalR.Owin" version="1.0.1" targetFramework="net40" />
<package id="Microsoft.AspNet.SignalR.SystemWeb" version="1.0.1" targetFramework="net40" />