简体   繁体   English

WebApi无法序列化从Exception派生类声明的属性

[英]WebApi can't serialize attribute declared on derived class from Exception

I can´t get the properties values created on BaseException when call to Get api method. 调用Get api方法时,无法获取在BaseException上创建的属性值。 Any idea why? 知道为什么吗?

public class BaseException : Exception
{
    public  string ExType { get; set; }

    public JObject Properties { get; set; }

    public Guid ErrorCodeId { get; set; }

    public BaseException(string message): base(message) { }
}

public class BadRequestException : BaseException
{
    public BadRequestException(string message) : base(message) { }
}

// GET: api/<controller>
public virtual IHttpActionResult Get()
{
    IHttpActionResult result = null;
    try
    {
        throw new Exception("Error description here");
        result = Ok();
    }
    catch (Exception ex)
    {
        result = ResponseMessage(Request.CreateResponse(HttpStatusCode.BadRequest, new BadRequestException(ex.Message)
        {
            ExType = "Any exception type"//Can't get this value in the output JSON
        }));
    }
    return result;
}

ExType value is not showing. ExType值未显示。 The result that I got is the following: 我得到的结果如下:

{
  "ClassName": "BadRequestException",
  "Message": "Error description here",
  "Data": null,
  "InnerException": null,
  "HelpURL": null,
  "StackTraceString": null,
  "RemoteStackTraceString": null,
  "RemoteStackIndex": 0,
  "ExceptionMethod": null,
  "HResult": -2146233088,
  "Source": null,
  "WatsonBuckets": null
}

There is any way to get the serealized value of my own properties? 有什么方法可以获取我自己财产的抵押价值?

Well regarding to this answer What is the correct way to make a custom .NET Exception serializable? 关于此答案, 那么使自定义.NET Exception可序列化的正确方法是什么? .

Is needed to explicitly add the new properties when serializing an object of custom inherited class from Exception. 序列化从Exception继承的自定义继承类的对象时,需要显式添加新属性。 To do that we should override the GetObjectData method and put into info all properties and values that you want to serialize. 为此,我们应该重写GetObjectData方法,并将要序列化的所有属性和值放入info中。

public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
    base.GetObjectData(info, context);
}

Great, so to automate that we could use reflection as follows 太好了,要自动化,我们可以按如下方式使用反射

public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
    //Getting first from base
    base.GetObjectData(info, context);

    if (info != null)
    {
        foreach (PropertyInfo property in this.GetType().GetProperties())
        {
            //Adding only properties that not already declared on Exception class
            if (property.DeclaringType.Name != typeof(Exception).Name)
            {
                info.AddValue(property.Name, property.GetValue(this));
            }
        }
    }
}

Then when serializing all custom properties apear in the output. 然后,在序列化所有自定义属性时,将出现在输出中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法捕获派生类中引发的异常 - Can't catch exception thrown in derived class 从BindingList派生的类的公共字段/属性 <T> 不会序列化 - Public fields/properties of a class derived from BindingList<T> wont serialize 序列化从不可序列化的类派生的类 - Serialize a class that is derived from a non serializable class WebApi无法序列化实体框架实体 - WebApi can't serialize Entity Framework entities 如何序列化从ReadOnlyCollection派生的类 - How to Serialize class derived from ReadOnlyCollection 从基类序列化和反序列化派生类 - Serialize and DeSerialized derived classes from the base class 二进制序列化.NET异常派生类; 使用FluentAssertions进行单元测试 - Binary serialize a .NET Exception-derived class; unit testing with FluentAssertions 使用派生嵌套类的字符串列表将类对象序列化为C#Webapi中的XML - Serialize class object with list of strings derived nested class into XML in c# Webapi 为什么Json.net不能正确地序列化从TreeNode派生的类? - Why doesn't Json.net properly serialize a class derived from TreeNode? ProtoBuf在WebApi HttpResponseMessage中序列化异常 - ProtoBuf serialize exception in WebApi HttpResponseMessage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM