I have this exception of type System.AggregateException :
Message = "One or more errors occurred." Source = null StackTrace = null
This InnerException is 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)
This InnerException is the exception I created that extends from 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
When I ran this:
Exception excpetion = ex.GetBaseException();
Where ex is System.AggregateException , I get a System.Reflection.TargetInvocationException in excpetion . How can this happen?
Scenario
I do not know how to reproduce this in a simple project. In my case, I found this with a SignalR project. Some hub methods throw an exception, handle errors with this in global.asax:
GlobalHost.HubPipeline.AddModule(new MyHubPipelineModule());
And MyHubPipelineModule should look like this:
public class MyHubPipelineModule : HubPipelineModule { protected override void OnIncomingError(Exception ex, IHubIncomingInvokerContext context) { Exception excpetion = ex.GetBaseException(); context.Hub.Clients.Caller.ExceptionHandler(excpetion.Message); } }
Note. This should be done using SignalR 1.0.1. In version 1.1.0, the exception is simpler (smaller chain), so it works well. Make sure you have these package versions:
<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" />