title: Serializing an Exception Return a Very Large JSON
body: <p>I'm migrating an application from full framework to net core.
  I'm struggling to get the exception manager for the apis to get to work.</p>
  
  <p>I'm currently using a attribute based solution.</p>
  
  <p>I have this class</p>
  
  <pre><code>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);
          }
      }
  }
  </code></pre>
  
  <p>and I use the corresponding attribute on the desired controller.
      <code>[ApiExceptionFilter]</code></p>
  
  <p>The thing is, that when I catch the exception and throw it to the client as a JSON string. I got a 27mb download, and its because it's serializing the <code>TargetSite</code> and within of that it serialize the DefinedTypes which is a list of assemblies and types.</p>
  
  <p>Does somebody know how can I serialize the exception avoiding those properties?</p>
  
  <p>Here is an example of the exception:</p>
  
  <pre><code>{
  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",
  ..........
  </code></pre>
  
  <p>this list of defined types, has like a million of items... :(</p>