WCF: Throwing Exceptions With WebHttpBinding
来源:互联网 发布:js做一个黑色正方形 编辑:程序博客网 时间:2024/05/29 17:06
One of the very cool new features in Windows Communication Foundation is the ability to create REST or POX services. Unfortunately, you will quickly find that WebHttpBinding and WebHttpBehavior (both the binding and behavior that are used to pull off the REST operations in WCF) will swallow your exceptions, and worse, leave you with a very generic error message, like the following:
Request Error
The server encountered an error processing the request. See server logs for more details.
This is no good when your client is expecting back XML or a fault code! How to get around this? Implement your own WebHttpBehavior and add a custom error handler. This will let you return a message of your choice. The code below will return a standard SOAP-like fault message. Note that you can use Message.CreateMessage and XmlDocument to create a differently formatted message if you want.
Also, this example wraps ALL exceptions, regardless of their type. The standard WCF way of protecting sensitive server-side information (like stack traces) is to use FaultException, or FaultException<T> in your code, and only show the message if the exception is of one of those types. I would suggest adding this type filter if you care about your server-side intellecual property rights.
FaultingWebHttpBehavior:
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Text;
5: using System.ServiceModel.Description;
6: using System.ServiceModel.Dispatcher;
7: using System.ServiceModel.Channels;
8: using System.Xml;
9: using System.ServiceModel;
10:
11: namespace Decav.FreeCode.ServiceModel
12: {
13: /// <summary>
14: /// A <see cref="WebHttpBehavior"/> that does not attempt to display friendly error messages when an exception occurs
15: /// locating or invoking a service.
16: /// </summary>
17: public class FaultingWebHttpBehavior : WebHttpBehavior
18: {
19: protected override void AddServerErrorHandlers(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
20: {
21: endpointDispatcher.ChannelDispatcher.ErrorHandlers.Clear();
22: endpointDispatcher.ChannelDispatcher.ErrorHandlers.Add(new ErrorHandler());
23: }
24:
25: public class ErrorHandler : IErrorHandler
26: {
27: #region IErrorHandler Members
28:
29: public bool HandleError(Exception error)
30: {
31: return true;
32: }
33:
34: public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
35: {
36: FaultCode faultCode = FaultCode.CreateSenderFaultCode(error.GetType().Name, "http://tempuri.org/net/exceptions");
37: fault = Message.CreateMessage(version, faultCode, error.Message, null);
38: }
39:
40: #endregion
41: }
42: }
43: }
FaultingWebHttpBehaviorElement (this is the configuration element used in the app.config:
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Text;
5: using System.ServiceModel.Configuration;
6:
7: namespace Decav.FreeCode.ServiceModel
8: {
9: /// <summary>
10: /// The configuration element for a <see cref="FaultingWebHttpBehavior"/>.
11: /// </summary>
12: public class FaultingWebHttpBehaviorElement : BehaviorExtensionElement
13: {
14: /// <summary>
15: /// Gets the type of behavior.
16: /// </summary>
17: /// <value></value>
18: /// <returns>A <see cref="T:System.Type"/>.</returns>
19: public override Type BehaviorType
20: {
21: get { return typeof(FaultingWebHttpBehavior); }
22: }
23:
24: /// <summary>
25: /// Creates a behavior extension based on the current configuration settings.
26: /// </summary>
27: /// <returns>The behavior extension.</returns>
28: protected override object CreateBehavior()
29: {
30: return new FaultingWebHttpBehavior();
31: }
32: }
33: }
App.config:
1: <configuration>
2: <system.serviceModel>
3: <services>
4: <service name="Decav.FreeCode.SomeService">
5: <endpoint address=""
6: behaviorConfiguration="WebBehavior"
7: binding="webHttpBinding"
8: contract="Decav.FreeCode.SomeService"/>
9: <host>
10: <baseAddresses>
11: <add baseAddress="http://localhost/Services/SomeService"/>
12: </baseAddresses>
13: </host>
14: </service>
15: </services>
16: <behaviors>
17: <endpointBehaviors>
18: <behavior name="WebBehavior">
19: <faultingWebHttp/>
20: </behavior>
21: </endpointBehaviors>
22: </behaviors>
23: <extensions>
24: <behaviorExtensions>
25: <!-- NOTE: Fully qualified name required, see: https://connect.microsoft.com/wcf/feedback/ViewFeedback.aspx?FeedbackID=216431 -->
26: <add name="faultingWebHttp" type="Decav.FreeCode.ServiceModel.FaultingWebHttpBehaviorElement, Decav.FreeCode, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
27: </behaviorExtensions>
28: </extensions>
29: </system.serviceModel>
30: </configuration>
- WCF: Throwing Exceptions With WebHttpBinding
- wcf webhttpbinding
- Throwing and Catching Exceptions
- use webHttpBinding to implement WCF Rest Service
- Unhandled exceptions in WCF
- Programming with Exceptions
- Error Handling with exceptions
- 关于WCF REST(webHttpBinding ) 用法:服务端写法及客户端调用
- The Trouble with Checked Exceptions
- Error Handling with Exceptions【1】
- Error Handling with Exceptions【2】
- Error Handling with Exceptions【3】
- Error Handling with Exceptions【4】
- Error Handling with Exceptions【5】
- Simplifying WCF: Using Exceptions as Faults
- Handling Disconnects with WCF
- 第九章: Error Handling with Exceptions
- Error Handling with C++ Exceptions, Part 1
- 极限编程最佳实践的深入研究
- C语言例题6:
- windbg 的常用命令--强大!常用!
- 我的EAI解决方案
- 我的偶像Micheal.Jackson (MTV全集):本来是有一套Micheal的经典VCD的,但被朋友拿走再没还回来……
- WCF: Throwing Exceptions With WebHttpBinding
- 使用msxml遍历重复的node问题
- windbg的中文帮助文档
- 一种Mobile UI及应用系统的构架与设计
- ExtJs自学教程(2):从DOM看EXTJS
- ucosII学习笔记
- JSP中自定义标记库的使用(二)
- 政协第十一届全国委员会二次会议日程公布
- Java语言 -- 多态