标签: targetinvocationexception

为什么IDE会将TargetInvocationException视为未捕获?

我有一些代码使用反射来从对象中提取属性值.在某些情况下,属性可能会抛出异常,因为它们具有空引用等.

object result;
try
{
    result = propertyInfo.GetValue(target, null);

}
catch (TargetInvocationException ex)
{
    result = ex.InnerException.Message;
}
catch (Exception ex)
{
    result = ex.Message;
}
Run Code Online (Sandbox Code Playgroud)

最终代码正常工作,但是当我在调试器下运行时:

当属性抛出异常时,IDE将进入调试器,就好像异常未被捕获一样.如果我刚刚运行,程序就会流出,异常作为TargetInvocationException出现,并在InnerException属性中有真正的异常.

我怎么能阻止这种情况发生?

c# reflection exception propertyinfo targetinvocationexception

25
推荐指数
1
解决办法
2万
查看次数

如何在不丢失堆栈跟踪的情况下重新抛出TargetInvocationException的内部异常

我有许多方法正在调用Delegate.DynamicInvoke.其中一些方法会进行数据库调用,我希望能够捕获一个SqlException而不是TargetInvocationException通过它的内部捕获并查找实际出错的方法.

我正在使用此方法重新抛出但它清除了堆栈跟踪:

 try
 {
      return myDelegate.DynamicInvoke(args);
 }
 catch(TargetInvocationException ex)
 {
     Func<TargetInvocationException, Exception> getInner = null;
     getInner =
        delegate(TargetInvocationException e)
        {
        if (e.InnerException is TargetInvocationException)
            return getInner((TargetInvocationException) e.InnerException);

         return e.InnerException;
        };

     Exception inner = getInner(ex);
     inner.PreserveStackTrace();
     throw inner;
 }
Run Code Online (Sandbox Code Playgroud)

这个PreserveStackTrace方法是我通过另一个帖子修复的扩展方法(我不知道它实际上做了什么).但是,这似乎不会保留跟踪:

public static void PreserveStackTrace(this Exception e)
{
    var ctx = new StreamingContext(StreamingContextStates.CrossAppDomain);
    var mgr = new ObjectManager(null, ctx);
    var si = new SerializationInfo(e.GetType(), new FormatterConverter());

    e.GetObjectData(si, ctx);
    mgr.RegisterObject(e, 1, si);
    mgr.DoFixups(); 
}
Run Code Online (Sandbox Code Playgroud)

c# exception-handling stack-trace targetinvocationexception inner-exception

20
推荐指数
1
解决办法
5427
查看次数

ASP.NET死黄色屏幕 - 它从哪里获得堆栈跟踪?

我在我的应用程序中设置了远程类型,我避免TargetInvocationExceptions并抓住内部异常.我调用类的内部PrepForRemoting方法Exception来保护调用方法的堆栈跟踪.

这似乎正确构造堆栈跟踪属性:

"\ r \n服务器堆栈跟踪:\ r \n

在ZBooking.Environment.Services.BookingService.<> c_ DisplayClass9`1.b _5(BookingSlot p)在C:\ dev\ZBookings\core\ZZBookings.Services\BookingService.cs:第79行\ r \n

在System.Linq.Enumerable.All [TSource](IEnumerable'1 source,Func'2谓词)\ r \n

在ZBookings.BookingService.MoveBooking [TBookingType](Int32 bookingId,> IEnumerable`1 bookingSlots)在C:\ dev\ZBooking.Client\core\ZBookings.Services\BookingService.cs:第79行\ r \n\r \n

在[0]处重新抛出异常:\ r \n在ZBookings.BookingService.<> c_ DisplayClass9`1.b _5(BookingSlot p)在C:\ dev\ZBookings\core\ZBookings.Services\BookingService.cs:第79行\ [R \n

在System.Linq.Enumerable.All [TSource](IEnumerable'1 source,Func'2谓词)\ r \n

在ZBookings.BookingService.MoveBooking [TBookingType](Int32 bookingId,IEnumerable`1 bookingSlots)在C:\ dev\ZBookings\core\ZBookings.Services\BookingService.cs:第79行"

但是,当标准ASP.NET黄色屏幕显示时,它是:

[NullReferenceException:对象引用未设置为对象的实例.] ZBooking.ApplicationServices.MethodMarshaller.Invoke(Delegate del,ZipIdentity zipIdentity,Object [] args)在C:\ dev\ZBooking\core\ZBooking.ApplicationServices\MethodMarshaller中. cs:147 ZBooking.ApplicationServices.MethodMarshaller.Invoke(Delegate del,ZipIdentity zipIdentity,Object [] args)在C:\ dev\ZBooking\core\ZBooking.ApplicationServices\MethodMarshaller.cs:105 ZBooking.ApplicationServices.MethodMarshaller.Call(Func) '3 del,T1 arg1,T2 arg2,ZipIdentity zipIdentity)在C:\ dev\ZBooking\core\ZBooking.ApplicationServices\MethodMarshaller.cs:72
...等.

Server.GetLastError();在Global.asax中调用Application_Error会显示正确的堆栈跟踪.黄色屏幕堆栈的痕迹来自哪里?

c# asp.net exception targetinvocationexception ysod

7
推荐指数
1
解决办法
1229
查看次数

w3wp随服务器WV调用AccessViolationException和TargetInvocationException随机崩溃

我们在EventLog中随机观察(如1-5天一次)IIS崩溃异常.
服务器 - Windows Server 2012 R2.

  • 第一个是访问冲突(更常见):

应用程序:w3wp.exe
Framework版本:v4.0.30319
描述:由于未处理的异常,进程已终止.
异常信息:System.AccessViolationException
Stack:

这个没有其他细节.

  • 另一个是目标调用(不常用):

应用程序:w3wp.exe
Framework版本:v4.0.30319
描述:由于未处理的异常,进程已终止.
异常信息:System.Reflection.TargetInvocationException
Stack:
at System.ReunlectionMethodHandle.InvokeMethod(System.Object,System.Object [],System.Signature,Boolean)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(System.Object,System.Object [ ],System.Object的[])
在System.Delegate.DynamicInvokeImpl(System.Object的[])
...(各种堆栈跟踪进一步)...

我们已经使用此TargetInvocationException的一些变体执行了一些调试,并且通过在特定位置强制抛出异常,我们可以重复其中一个堆栈跟踪,但它有一个内部异常(这里没有)+没有崩溃IIS并且通常将错误返回到FaultException中包装的客户端.

您认为它可能与.NET 4.0处理损坏的进程状态的变化有关吗?https://msdn.microsoft.com/en-us/library/system.runtime.exceptionservices.handleprocesscorruptedstateexceptionsattribute(v=vs.110).aspx

iis wcf access-violation targetinvocationexception

5
推荐指数
0
解决办法
728
查看次数

尝试从SSIS包保存大于~1.5 MiB的文件时,EPPlus 2.9.0.1抛出System.IO.IsolatedStorage.IsolatedStorageException

问题

当我尝试使用EPPlus保存文件超过~1.5 MiB时ExcelPackage.Save()抛出一个System.IO.IsolatedStorage.IsolatedStorageException.

说明

我与创建一个SSIS包Visual Studio 2008 9.0.30729.4462 QFE,并.NET Framework 3.5 SP1到的内容导出SQL Server 2008 SP2 10.0.4311.0 64 bit通过表EPPlus 2.9.0.1库.

SSIS包非常简单:Execute SQL Task它读取表的内容并将其放在变量中,然后Script task读取记录集变量并通过EPPlus将内容保存到磁盘.

脚本任务的代码是:

namespace ST_00a0b40814db4c7290b71f20a45b62c6.csproj
{
    using System;
    using System.AddIn;
    using System.Data;
    using System.Data.OleDb;
    using System.IO;
    using Microsoft.SqlServer.Dts.Runtime;
    using Microsoft.SqlServer.Dts.Tasks.ScriptTask;
    using OfficeOpenXml;

    [AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
    public partial class ScriptMain : VSTARTScriptObjectModelBase
    {
        public void Main()
        { …
Run Code Online (Sandbox Code Playgroud)

c# ssis targetinvocationexception epplus

5
推荐指数
1
解决办法
5772
查看次数

Activator.CreateInstance 上的 TargetInvocationException

我在使用反射实例化 DLL 文件中定义的类时遇到了一些麻烦。尽管代码看起来不错,但我在 Activator.CreateInstance 上得到了 TargetInvocationException。代码如下(目前只有一个 DLL 可用):

public Comparator() 
{
    string[] filePaths = Directory.GetFiles(@".\Extensions");
    for (int i = 0; i < filePaths.Length; ++i)
    {
        var asm = Assembly.LoadFrom(Path.GetFullPath(filePaths[i]));

        if (asm != null)
        {
            foreach (Type currType in asm.ExportedTypes)
            {
                if (!currType.IsAbstract && currType.IsPublic && typeof(SorterBase).IsAssignableFrom(currType))
                {
                    Debug.WriteLine(currType); //outputs: BubbleSort.Sorter
                    var instance = Activator.CreateInstance(currType); //TargetInvocationException
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我试图实例化的类:

using SortingLibrary;
using SortingFunctions;
namespace BubbleSort
{
    public class Sorter : SorterBase //SorterBase is an abstract class defined in …
Run Code Online (Sandbox Code Playgroud)

c# createinstance activator targetinvocationexception

3
推荐指数
1
解决办法
2854
查看次数