有没有一种方法可以反序列化Exception对象?

时间:2019-01-17 20:18:29

标签: c# exception json.net deserialization

我正在研究WCF REST项目。在WCF /服务器端,每当我的catch块中发生异常时,我都会在下面进行操作,并在对客户端的响应中将异常作为json字符串发送。

public MyResponse GetData(MyRequest request)
{
   MyResponse response = new MyResponse();
   try
   {
       // do something
   }
   catch(Exception ex)
   { 
      response.success = false;
      response.ExceptionJSONString = Newtonsoft.Json.JsonConvert.SerializeObject(ex);
   }
   return response;
}

请在下面查看我的客户端

enter image description here

我的问题是有一种反序列化Exception对象的方法。我觉得您不能做,因为Exception类继承了ISerializable。但是只是想问一下,看看有人做过。

更新:我能够像下面这样从客户端到服务器获取确切的异常对象

在DataContract类下面创建

[DataContract]
public class MyExceptionJson
{
   [DataMember]
   public string JsonData { get; set; }

   [DataMember]
   public string AssemblyName { get; set; }

   [DataMember]
   public string TypeName { get; set; }

   public MyExceptionJson()
   {
      JsonData = string.Empty;
      AssemblyName = string.Empty;
      TypeName = string.Empty;
   }

   public MyExceptionJson(Exception exception)
   {
      JsonData = Newtonsoft.Json.JsonConvert.SerializeObject(exception);
      Type type = exception.GetType();
      AssemblyName = type.Assembly.GetName().Name;
      TypeName = type.FullName;
   }

    public Exception ToException()
    {
       if (string.IsNullOrEmpty(JsonData) == true ||
           string.IsNullOrEmpty(AssemblyName) == true ||
           string.IsNullOrEmpty(TypeName) == true)
         {
            return new Exception();
         }

       Type type = null;
      foreach (Assembly item in System.AppDomain.CurrentDomain.GetAssemblies())
      {
         AssemblyName assemblyName = item.GetName();
          if (assemblyName != null &&
              string.IsNullOrEmpty(assemblyName.Name) == false &&
              assemblyName.Name == AssemblyName)
            {
               type = item.GetType(TypeName);
               if (type != null)
                   break;
             }
        }
        //fail safe code
        if (type == null)
        {
           type = typeof(Exception);
        }
     object returnException = Newtonsoft.Json.JsonConvert.DeserializeObject(JsonData, type);
     return returnException as Exception;
     }
}

在我的响应类中添加了该类类型的属性,如下所示

[DataContract]
public class MyResponse 
{
   [DataMember]
   public bool Success { get; set; }

   [DataMember]
   public MyExceptionJson ExceptionDataAsJson { get; set; }
}

服务器:发生异常时

  MyResponse response = new MyResponse()
  {
     Success = false,
     ExceptionDataAsJson = null
  };

  try 
  {
     //code 
  }
  catch(Exception ex)
  {
      response.ExceptionDataAsJson = new MyExceptionJson(ex);
  }
  return response;

客户:收到回复后

 if (response != null && response.Success == false)
 {
  Exception ex = null;
  //something went wrong                    
  if (response.ExceptionDataAsJson != null)
  {
    ex = response.ExceptionDataAsJson.ToException();                        
  }
 }

1 个答案:

答案 0 :(得分:2)

您可以只使用printf("%s", mess[0]); 因此,将您的代码更改为此:

Newtonsoft.Json.JsonConvert.DeserializeObject<T>