Exception.GetBaseException()返回不为null的异常InnerException [英] Exception.GetBaseException() returning exception with not null InnerException

查看:115
本文介绍了Exception.GetBaseException()返回不为null的异常InnerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的异常类型为 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

It's InnerException is this exception created by me 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

运行此命令时:

Exception excpetion = ex.GetBaseException();

其中 ex System.AggregateException 我在执行时得到了 System.Reflection.TargetInvocationException 这怎么发生?

Where ex is the System.AggregateException I get the System.Reflection.TargetInvocationExceptionat excpetion. How can this happen?

场景

我不知道如何在一个简单的项目中重现它。就我而言,我在SignalR项目中发现了这一点。一些中心方法引发异常,并在global.asax上处理此错误:

I don't know how to reproduce this in a simple project. In my case I found this with a SignalR project. Some hub method throwing an exception an handle the errors with this at the 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" />


推荐答案

问题似乎与SignalR无关。它与 AggregateExceptions 有关。

It seemed that the problem wasn't linked with SignalR. It has to do with AggregateExceptions.

我在看 AggregateException.GetBaseException方法的MSDN页面,我发现了这一点:

I was looking at the MSDN page of AggregateException.GetBaseException Method and I found this:


返回该异常的根本原因的AggregateException

Returns the AggregateException that is the root cause of this exception

Exception.GetBaseException的文档方法仅在未重写方法时有效。

So I guess that the documentation of Exception.GetBaseException Method is valid only if the method is not overridden.

这篇关于Exception.GetBaseException()返回不为null的异常InnerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆