具有附加属性的自定义异常类不会序列化

时间:2020-01-16 13:43:19

标签: c# exception .net-core

我们创建了一个自定义异常类

  [Serializable]
  public class ApplicationError : Exception
  {
      public ApplicationError()
      {
          // Add any type-specific logic, and supply the default message.
      }

      public ApplicationError(string message) : base(message)
      {
          // Add any type-specific logic.
      }
      public ApplicationError(string message, Exception innerException) :
         base(message, innerException)
      {
          // Add any type-specific logic for inner exceptions.
      }
      protected ApplicationError(SerializationInfo info,
         StreamingContext context) : base(info, context)
      {
          // Implement type-specific serialization constructor logic.
      }


      public string RequestId { get; set; }
      public string ErrorMessge { get; set; }
      public DateTime ErrorOccurredTime { get; set; }
      public string Environment { get; set; }
      public string PageURL { get; set; }


  }

用于向中间件引发异常,然后将其记录。

public async Task Invoke(HttpContext context /* other dependencies */)
        {
            try
            {
                await next(context);
            }
            catch(ApplicationError exception)
            {
                var message = JsonConvert.SerializeObject(exception);
                _logger.LogError(exception, message);
                await HandleExceptionAsync(context, exception);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex,ex.Message);
                await HandleExceptionAsync(context, ex);
            }
        }

像这样从服务抛出异常:

private async Task<TResult> SendRequestAsync<TResult>(string url, HttpMethod httpMethod, object body = null)
        {
            var retrieveResponse = (TResult)Activator.CreateInstance(typeof(TResult));

            var response = await _httpService.SendAuthenticatedRequestAsync(url, httpMethod, body);

            if (response.IsSuccessStatusCode)
            {
                retrieveResponse = await response.Content.ReadAsAsync<TResult>();
            }
            else
            {
                var error = await response.Content.ReadAsAsync<ErrorResponse>();

                ApplicationError applicationError = new ApplicationError
                {
                    RequestId = error.RequestId,
                    ErrorMessge = error.Message,
                    ErrorOccurredTime = DateTime.Now,
                    PageURL = string.Empty,
                    Environment = _hostingEnvironment.EnvironmentName
                };

                throw applicationError;
        }
return retrieveResponse;
}

当异常在记录异常之前到达中间件和内部类时,对象将被序列化,但是自定义属性在序列化中丢失。

 catch(ApplicationError exception)
            {
                var message = JsonConvert.SerializeObject(exception);
//message does not have custom properies.
                _logger.LogError(exception, message);
                await HandleExceptionAsync(context, exception);
            }

我缺少什么?

0 个答案:

没有答案