我正在将应用程序从完整框架迁移到网络核心。 我正在努力为API启用异常管理器。
我目前正在使用基于属性的解决方案。
我有这堂课
public class ApiExceptionFilterAttribute : ExceptionFilterAttribute
{
public override async void OnException(ExceptionContext context)
{
context.HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
var ex = context.Exception;
if (ex == null) return;
context.HttpContext.Response.ContentType = "application/json";
using (var writer = new StreamWriter(context.HttpContext.Response.Body))
{
var js = new JsonSerializer();
js.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
js.Serialize(writer, ex);
await writer.FlushAsync().ConfigureAwait(false);
}
}
}
,然后在所需的控制器上使用相应的属性。
[ApiExceptionFilter]
问题是,当我捕获异常并将其作为JSON字符串扔给客户端时。我下载了一个27mb的文件,其原因是它要对TargetSite
进行序列化,并在其中进行序列化DefinedTypes,这是程序集和类型的列表。
有人知道如何避免这些属性序列化异常吗?
以下是例外情况的一个示例:
{
Id: null,
Message: "Hubo errores en la validación de los parámetros de la operación.",
Data: { },
InnerException: {
Detail: {
Details: [
{
Key: "Cuit",
Message: "The field Cuit must match the regular expression '^[0-9]{11}$'.",
ParameterName: "request"
}
]
},
Action: null,
Code: {
IsPredefinedFault: true,
IsSenderFault: true,
IsReceiverFault: false,
Namespace: "http://schemas.xmlsoap.org/soap/envelope/",
Name: "Client",
SubCode: null
},
Message: "Hubo errores en la validación de los parámetros de la operación.",
Reason: {
Translations: [
{
XmlLang: "en-US",
Text: "Hubo errores en la validación de los parámetros de la operación."
}
]
},
Data: { },
InnerException: null,
TargetSite: {
Name: "HandleReply",
DeclaringType: "System.ServiceModel.Channels.ServiceChannel, System.Private.ServiceModel, Version=4.5.0.3, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
ReflectedType: "System.ServiceModel.Channels.ServiceChannel, System.Private.ServiceModel, Version=4.5.0.3, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
MemberType: 8,
MetadataToken: 100671507,
Module: {
MDStreamVersion: 131072,
FullyQualifiedName: "C:\Users\rotha\.nuget\packages\system.private.servicemodel\4.5.3\runtimes\win\lib\netstandard2.0\System.Private.ServiceModel.dll",
ModuleVersionId: "c5851175-5eba-412a-8f03-6cd647a2a897",
MetadataToken: 1,
ScopeName: "System.Private.ServiceModel.dll",
Name: "System.Private.ServiceModel.dll",
Assembly: {
CodeBase: "file:///C:/Users/xxx/.nuget/packages/system.private.servicemodel/4.5.3/runtimes/win/lib/netstandard2.0/System.Private.ServiceModel.dll",
FullName: "System.Private.ServiceModel, Version=4.5.0.3, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
EntryPoint: null,
DefinedTypes: [
"FxResources.System.Private.ServiceModel.SR, System.Private.ServiceModel, Version=4.5.0.3, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
"System.NotImplemented, System.Private.ServiceModel, Version=4.5.0.3, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
"System.__HResults, System.Private.ServiceModel, Version=4.5.0.3, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
"System.FxTrace, System.Private.ServiceModel, Version=4.5.0.3, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
"System.SR, System.Private.ServiceModel, Version=4.5.0.3, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
"System.Xml.XmlBinaryNodeType, System.Private.ServiceModel, Version=4.5.0.3, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
"System.Collections.Generic.KeyedByTypeCollection`1, System.Private.ServiceModel, Version=4.5.0.3, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
"System.Collections.Generic.SynchronizedCollection`1, System.Private.ServiceModel, Version=4.5.0.3, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
"System.Collections.Generic.SynchronizedKeyedCollection`2, System.Private.ServiceModel, Version=4.5.0.3, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
"System.Collections.Generic.SynchronizedReadOnlyCollection`1, System.Private.ServiceModel, Version=4.5.0.3, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
..........
这个已定义类型的列表,有大约一百万个项目...:(
答案 0 :(得分:0)
无需使用任何编写器来编写它,只需使用context.Result
:
public class ApiExceptionFilterAttribute : ExceptionFilterAttribute
{
public override void OnException(ExceptionContext context)
{
context.HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
var ex = context.Exception;
if (ex == null) return;
context.Result = new JsonResult(ex);
}
}
答案 1 :(得分:0)
这应该在Json.NET的Release 12.0.1中修复:
更改-序列化没有SerializableAttribute的异常时排除了TargetSite
此修复程序于2018年10月27日在提交 Exclude TargetSite from non-serializable Exception serialization (#1897) 中完成。
该问题似乎已在Json.NET 11.0 Release 1中引入:
更改-实现ISerializable但不具有[SerializableAttribute]的类型不会使用ISerializable进行序列化
有关为何最初对11.0.1进行更改的讨论,请参见this answer到 Deserializing custom exceptions in Newtonsoft.Json 。