有没有一种方法来反序列化的Exception对象?

问题描述 投票:-1回答:1

我工作的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

我的问题是有没有办法来反序列化异常对象。我觉得你不能这样做,因为异常类继承了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();                        
  }
 }
c# exception json.net deserialization
1个回答
2
投票

你可以只用Newtonsoft.Json.JsonConvert.DeserializeObject<T>所以你的代码改成这样:

Exception ex = Newtonsoft.Json.JsonConvert.DeserializeObject<Exception>(upResponse.ExceptionJSONString);
© www.soinside.com 2019 - 2024. All rights reserved.