重新创建是在一个JSON响应序列异常 [英] Recreate an exception that is serialized in a JSON response

查看:324
本文介绍了重新创建是在一个JSON响应序列异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在网络服务发生异常和ASP.NET Web API返回异常的JSON响应,像下面这样:

  HTTP / 1.1 500内部服务器错误
内容类型:应用程序/ JSON的;字符集= UTF-8{消息:发生了错误。
  ExceptionMessage:附近有语法错误fooBar'的。
  ExceptionType:System.Data.SqlClient.SqlException,
  堆栈跟踪:在System.Web.Http.ApiController.blah.blah.blah}

我想重新在客户端异常。我想给SQLException对象的响应转换(在下面的例子中),然后抛出异常。一些博客已经使用Activator.CreateInstance()和Type.GetType()用于创建运行时与字符串类型名称的对象提到有的已提到使用动态。然而,我无法弄清楚如何正确地使用它。我想AP preciate如果有人能教我。谢谢!

 公共类ExceptionResponse
{
    公共字符串消息{搞定;组; }
    公共字符串ExceptionType {搞定;组; }
    公共字符串ExceptionMessage {搞定;组; }
    公共字符串堆栈跟踪{搞定;组; }
}ExceptionResponse响应= httpContent.ReadAsAsync< ExceptionResponse方式>()结果;
键入exceptionType = Type.GetType(response.ExceptionType);
扔Activator.CreateInstance(exceptionType,新的对象[]);// Visual Studio中显示错误:类型捕获或抛出必须从System.Exception派生


解决方案

据我所知,您尝试通过HTTP来处理异常而reciving作为JSON格式的消息。所以,你可以尝试序列化(解析)HTTP响应,并创建ExceptionResponse的新实例。例如:

 使用System.Web.Script.Serialization;

您的异常类将是这样的:

 公共类ExceptionResponse:异常{
    公共字符串ExceptionType {搞定;组; }
    公共字符串ExceptionMessage {搞定;组; }
}

中调用:

  VAR HTT presponse = @{信息:发生了错误,ExceptionMessage:附近有语法错误foob​​ar'命令,ExceptionType:System.Data.SqlClient.SqlException,堆栈跟踪:在System.Web.Http.ApiController.blah.blah.blah} ;。变种E =新的JavaScriptSerializer()反序列化< ExceptionResponse>(HTT presponse);
Ë扔掉;

An exception occurs in web service and ASP.NET Web API returns the exception in JSON response, like following:

HTTP/1.1 500 Internal Server Error
Content-Type: application/json; charset=utf-8

{ "Message":"An error has occurred.", 
  "ExceptionMessage":"Incorrect syntax near 'FooBar'.",
  "ExceptionType":"System.Data.SqlClient.SqlException", 
  "StackTrace":"at System.Web.Http.ApiController.blah.blah.blah" }

I would like to recreate the exception at client side. I want to convert the response to a SqlException object (in the example below) and then throw the exception. Some blogs have mentioned using Activator.CreateInstance() and Type.GetType() for creating an object in run-time with the type name in string and some have mentioned the use of dynamic. However, I am unable to figure how to use it properly. I would appreciate if someone can educate me. Thanks!

public class ExceptionResponse
{
    public string Message { get; set; }
    public string ExceptionType { get; set; }
    public string ExceptionMessage { get; set; }
    public string StackTrace { get; set; }
}

ExceptionResponse response = httpContent.ReadAsAsync<ExceptionResponse>().Result;
Type exceptionType = Type.GetType(response.ExceptionType);
throw Activator.CreateInstance(exceptionType, new object[]);

// Visual Studio indicates error: The type caught or throw must be derived from System.Exception

解决方案

As I understand, you try to handle an exception which reciving by http as a JSON-formatted message. So, you can try to serialize (parse) HTTP response and create a new instance of ExceptionResponse. For example:

using System.Web.Script.Serialization;

Your exception class will be like this:

public class ExceptionResponse : Exception {
    public string ExceptionType { get; set; }
    public string ExceptionMessage { get; set; }
}

Invoking:

var httpResponse = @"{ ""Message"":""An error has occurred."", ""ExceptionMessage"":""Incorrect syntax near 'FooBar'."", ""ExceptionType"":""System.Data.SqlClient.SqlException"",  ""StackTrace"":""at System.Web.Http.ApiController.blah.blah.blah"" }";

var e = new JavaScriptSerializer().Deserialize<ExceptionResponse>(httpResponse);
throw e;

这篇关于重新创建是在一个JSON响应序列异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆