WCF4.0 -- RESTful WCF Services 错误处理 (Error Handling)

REST WCF 的错误处理可用于从 WCF Web HTTP 服务返回指定 HTTP 状态代码的错误,并且返回的错误详细信息使用与操作相同的格式(如 XML 或 JSON)。在WCF 4.0中增加了WebFaultException<T>异常类,我们可以更加方便的处理错误,并能给予客户端详细的错误提示。

服务端代码:
[c-sharp] view plain copy print ?
  1. //当查不到User时,抛出WebFaultException,并提示NotFound及错误信息 
  2. [WebGet(UriTemplate="/Users/{name}")] 
  3. public User GetUser(string name) 
  4.     var user = Users.FirstOrDefault(u => u.Name == name); 
  5.     if (user == null
  6.         throw new WebFaultException<string>( 
  7.            string.Format("There is no user with the userName '{0}'.", name), 
  8.            HttpStatusCode.NotFound); 
  9.     else 
  10.         return user; 
//当查不到User时,抛出WebFaultException,并提示NotFound及错误信息 [WebGet(UriTemplate="/Users/{name}")] public User GetUser(string name) { var user = Users.FirstOrDefault(u => u.Name == name); if (user == null) throw new WebFaultException<string>( string.Format("There is no user with the userName '{0}'.", name), HttpStatusCode.NotFound); else return user; }
客户端代码:
1. 先包装 HttpWebRequest 的Get方法便于演示:
[c-sharp] view plain copy print ?
  1. static string RequestGet(string url) 
  2.     var req = HttpWebRequest.Create(url); 
  3.     using (var resp = req.GetResponse()) 
  4.     { 
  5.         using (var stream = resp.GetResponseStream()) 
  6.         { 
  7.             using (var sr = new StreamReader(stream)) 
  8.             { 
  9.                 return sr.ReadToEnd(); 
  10.             } 
  11.         } 
  12.     } 
static string RequestGet(string url) { var req = HttpWebRequest.Create(url); using (var resp = req.GetResponse()) { using (var stream = resp.GetResponseStream()) { using (var sr = new StreamReader(stream)) { return sr.ReadToEnd(); } } } }
2. 客户端请求REST服务,这里传给服务端一个错误名字:errorname,服务端查不到抛出异常。
[c-sharp] view plain copy print ?
  1. var resp = RequestGet("http://localhost:52402/Service1/Users/errorname"); 
  2. Console.WriteLine(resp); 
var resp = RequestGet("http://localhost:52402/Service1/Users/errorname"); Console.WriteLine(resp);
3. 当Response.StatusCode 不等于 200(成功) 的时候,GetResponse() 方法会抛出 WebException,我们通过捕获它获得异常的详细内容:(WebException.Response 可以获得出错时的Response,服务端的错误内容写在Response Body里)
[c-sharp] view plain copy print ?
  1. try 
  2.     // Request ... 
  3. catch (WebException ex) 
  4.     var errResp = ex.Response as HttpWebResponse; 
  5.     Console.WriteLine("StatusCode:{0}({1})", errResp.StatusCode, (int)errResp.StatusCode); 
  6.     using (var stream = errResp.GetResponseStream()) 
  7.     { 
  8.         using (var sr = new StreamReader(stream)) 
  9.         { 
  10.             Console.WriteLine("Content:{0}", sr.ReadToEnd()); 
  11.         } 
  12.     } 
try { // Request ... } catch (WebException ex) { var errResp = ex.Response as HttpWebResponse; Console.WriteLine("StatusCode:{0}({1})", errResp.StatusCode, (int)errResp.StatusCode); using (var stream = errResp.GetResponseStream()) { using (var sr = new StreamReader(stream)) { Console.WriteLine("Content:{0}", sr.ReadToEnd()); } } }
运行结果:
WCF4.0 -- RESTful WCF Services 错误处理 (Error Handling)_第1张图片

WebFaultException<T> 还支持返回复杂的“错误”对象,我们在服务端定义一个复杂的错误类:
[c-sharp] view plain copy print ?
  1. public class ComplexError 
  2.     public string ErrorCode { get; set; } 
  3.     public string ErrorMessage { get; set; } 
public class ComplexError { public string ErrorCode { get; set; } public string ErrorMessage { get; set; } }
在服务中抛出这个复杂错误:
[c-sharp] view plain copy print ?
  1. [WebGet(UriTemplate = "/Users2/{name}")] 
  2. public User GetUser2(string name) 
  3.     var user = Users.FirstOrDefault(u => u.Name == name); 
  4.     if (user == null
  5.         throw new WebFaultException<ComplexError>( 
  6.            new ComplexError 
  7.            { 
  8.                ErrorCode = "E001"
  9.                ErrorMessage = string.Format("There is no user with the userName '{0}'.", name) 
  10.            }, 
  11.            HttpStatusCode.NotFound); 
  12.     else 
  13.         return user; 
[WebGet(UriTemplate = "/Users2/{name}")] public User GetUser2(string name) { var user = Users.FirstOrDefault(u => u.Name == name); if (user == null) throw new WebFaultException<ComplexError>( new ComplexError { ErrorCode = "E001", ErrorMessage = string.Format("There is no user with the userName '{0}'.", name) }, HttpStatusCode.NotFound); else return user; }
服务端会按照既定的ResponseFormat返回错误信息。
运行结果:
WCF4.0 -- RESTful WCF Services 错误处理 (Error Handling)_第2张图片

PS: 如果使用 WCF REST Starter Kit 的 HttpClient 处理的话,需要自己判断 StatusCode。
[c-sharp] view plain copy print ?
  1. var client = new HttpClient(); 
  2. var resp = client.Get("http://localhost:52402/Service1/Users/errorname"); 
  3. Console.WriteLine("StatusCode:{0}({1})", resp.StatusCode, (int)resp.StatusCode); 
  4. var result = resp.Content.ReadAsString(); 
  5. Console.WriteLine("Content:{0}", result); 

你可能感兴趣的:(WCF4.0 -- RESTful WCF Services 错误处理 (Error Handling))