C# 如何修复“mscorlib.dll 中发生‘System.AggregateException’”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11122092/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to fix "'System.AggregateException' occurred in mscorlib.dll"
提问by javapowered
I'm receiving an unhandled exception while debugging, and the program stops executing. The debugger doesn't show me the line so I don't know what to fix.
我在调试时收到一个未处理的异常,程序停止执行。调试器没有向我显示该行,所以我不知道要修复什么。
An unhandled exception of type '
System.AggregateException' occurred inmscorlib.dllAdditional information: A Task's exception(s) were not observed either by Waiting on the Task or accessing its Exception property. As a result, the unobserved exception was rethrown by the finalizer thread.
Cannot obtain value of local or argument '
<this>' as it is not available at this instruction pointer, possibly because it has been optimized away.System.Threading.Tasks.TaskExceptionHolder
类型“
System.AggregateException”的未处理异常发生在mscorlib.dll附加信息:等待任务或访问其 Exception 属性均未观察到任务的异常。结果,未观察到的异常被终结器线程重新抛出。
无法获取本地或参数“
<this>”的值,因为它在此指令指针处不可用,可能是因为它已被优化掉。System.Threading.Tasks.TaskExceptionHolder
How can I troubleshoot this problem?
我该如何解决这个问题?
I also found this questionwhich is pretty similar.
我也发现this question非常相似。
采纳答案by SLaks
As the message says, you have a task which threw an unhandled exception.
正如消息所说,您有一个引发未处理异常的任务。
Turn on Break on All Exceptions (Debug, Exceptions) and rerun the program.
This will show you the original exception when it was thrown in the first place.
打开 Break on All Exceptions (Debug, Exceptions) 并重新运行程序。
这将向您显示最初抛出的原始异常。
(comment appended): In VS2015 (or above). Select Debug > Options > Debugging > Generaland unselect the "Enable Just My Code" option.
(附注):在 VS2015(或更高版本)中。选择调试 > 选项 > 调试 > 常规并取消选择“仅启用我的代码”选项。
回答by Darren
You could handle the exception directly so it would not crash your program (catching the AggregateException). You could also look at the Inner Exception, this will give you a more detailed explanation of what went wrong:
您可以直接处理异常,以免程序崩溃(捕获AggregateException)。您还可以查看内部异常,这将为您提供更详细的错误说明:
try {
// your code
} catch (AggregateException e) {
}
回答by helios456
The accepted answer will work if you can easily reproduce the issue. However, as a matter of best practice, you should be catching any exceptions (and logging) that are executed within a task. Otherwise, your application will crash if anything unexpected occurs within the task.
如果您可以轻松重现该问题,则接受的答案将起作用。但是,作为最佳实践,您应该捕获在任务中执行的任何异常(和日志记录)。否则,如果任务中发生任何意外,您的应用程序将崩溃。
Task.Factory.StartNew(x=>
throw new Exception("I didn't account for this");
)
However, if we do this, at least the application does not crash.
但是,如果我们这样做,至少应用程序不会崩溃。
Task.Factory.StartNew(x=>
try {
throw new Exception("I didn't account for this");
}
catch(Exception ex) {
//Log ex
}
)
回答by Nono
In my case I ran on this problem while using Edge.js — all the problem was a JavaScript syntax error inside a C# Edge.js function definition.
就我而言,我在使用 Edge.js 时遇到了这个问题——所有问题都是 C# Edge.js 函数定义中的 JavaScript 语法错误。
回答by Jose David Mosquera Luna
System.AggregateException HResult=0x80131500 Mensaje = Se han producido uno o varios errores. Origen = mscorlib Seguimiento de la pila: en System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions) en System.Threading.Tasks.Task1.GetResultCore(Boolean waitCompletionNotification) en System.Threading.Tasks.Task1.get_Result() en Itrebol.Presentation.FormularioTerceros.<>c__DisplayClass26_0.b__1(Task1 readTask) en C:\Users\GonzaloAlfonso\source\repos\ServicioItrebol - copia\Itrebol.Presentation\FormularioTerceros.xaml.cs: línea 535 en System.Threading.Tasks.ContinuationTaskFromResultTask1.InnerInvoke() en System.Threading.Tasks.Task.Execute() Excepción interna 1: JsonSerializationException: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[Itrebol.Dato.Modelo.TERCEROS]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path 'Message', line 1, position 21.
System.AggregateException HResult=0x80131500 Mensaje = Se han producido uno o varios 错误。Origen = mscorlib Seguimiento de la pila: en System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions) en System.Threading.Tasks.Task 1.GetResultCore(Boolean waitCompletionNotification) en System.Threading.Tasks.Task1.get_Result() en Itrebol.Presentation.FormularioTerceros.<>c__DisplayClass26_0.b__1(Task1 readTask) en C:\Users\GonzaloAlfonso\source\repos\ServicioItrebol - copia\Itrebol.Presentation\FormularioTerceros.xaml.cs: línea 535 en System.Threading.Tasks.ContinuationTaskFromResultTask1.InnerInvoke() zh-cn System.Threading.Tasks.Task.Execute() 内部异常 1:JsonSerializationException:无法将当前 JSON 对象(例如 {"name":"value"})反序列化为“System.Collections.Generic”类型。列出`1[Itrebol.Dato.Modelo.TERCEROS]' 因为该类型需要一个 JSON 数组(例如 [1,2,3])才能正确反序列化。要修复此错误,请将 JSON 更改为 JSON 数组(例如 [1,2,3])或更改反序列化类型,使其成为普通的 .NET 类型(例如,不是像整数这样的原始类型,而不是像这样的集合类型)可以从 JSON 对象反序列化的数组或列表)。JsonObjectAttribute 也可以添加到类型中以强制它从 JSON 对象反序列化。路径“消息”,第 1 行,位置 21。

