WCF: Throwing Exceptions With WebHttpBinding

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>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值