REST WCF 的错误处理可用于从 WCF Web HTTP 服务返回指定 HTTP 状态代码的错误,并且返回的错误详细信息使用与操作相同的格式(如 XML 或 JSON)。在WCF 4.0中增加了WebFaultException<T>异常类,我们可以更加方便的处理错误,并能给予客户端详细的错误提示。
服务端代码:
[c-sharp] view plain copy print ?
-
- [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;
- }
//当查不到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 ?
- 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();
- }
- }
- }
- }
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 ?
- var resp = RequestGet("http://localhost:52402/Service1/Users/errorname");
- 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 ?
- try
- {
-
- }
- 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());
- }
- }
- }
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()); } } }
运行结果:
WebFaultException<T> 还支持返回复杂的“错误”对象,我们在服务端定义一个复杂的错误类:
[c-sharp] view plain copy print ?
- public class ComplexError
- {
- public string ErrorCode { get; set; }
- public string ErrorMessage { get; set; }
- }
public class ComplexError { public string ErrorCode { get; set; } public string ErrorMessage { get; set; } }
在服务中抛出这个复杂错误:
[c-sharp] view plain copy print ?
- [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;
- }
[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返回错误信息。
运行结果:
PS: 如果使用 WCF REST Starter Kit 的 HttpClient 处理的话,需要自己判断 StatusCode。
[c-sharp] view plain copy print ?
- var client = new HttpClient();
- var resp = client.Get("http://localhost:52402/Service1/Users/errorname");
- Console.WriteLine("StatusCode:{0}({1})", resp.StatusCode, (int)resp.StatusCode);
- var result = resp.Content.ReadAsString();
- Console.WriteLine("Content:{0}", result);