首页 > 解决方案 > 序列化异常返回一个非常大的 JSON

问题描述

我正在将应用程序从完整框架迁移到网络核心。我正在努力让 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",
..........

这个定义类型的列表,有大约一百万个项目...... :(

标签: c#asp.net-core.net-corejson.net

解决方案


这应该在Json.NET的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 更改的原因的讨论,请参见Deserializing custom exceptions in Newtonsoft.Json这个答案


推荐阅读