Web服务中发生异常,ASP.NET Web API在JSON响应中返回异常,如下所示:
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" }
我想在客户端重新创建异常。我想将响应转换为SqlException对象(在下面的示例中),然后抛出异常。一些博客提到使用Activator.CreateInstance()和Type.GetType()在运行时创建一个对象,字符串中的类型名称,有些人提到使用动态。但是,我无法确定如何正确使用它。如果有人能教育我,我将不胜感激。谢谢!
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
答案 0 :(得分:0)
据我所知,您尝试处理一个异常,该异常通过http作为JSON格式的消息进行回收。因此,您可以尝试序列化(解析)HTTP响应并创建ExceptionResponse的新实例。例如:
using System.Web.Script.Serialization;
您的异常类将如下所示:
public class ExceptionResponse : Exception {
public string ExceptionType { get; set; }
public string ExceptionMessage { get; set; }
}
调用:
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;
答案 1 :(得分:0)