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>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章