Export (0) Print
Expand All

IServiceBehavior Interface

Provides a mechanism to modify or insert custom extensions across an entire service, including the ServiceHostBase.

Namespace:  System.ServiceModel.Description
Assembly:  System.ServiceModel (in System.ServiceModel.dll)

public interface IServiceBehavior

Implement IServiceBehavior to modify, examine, or extend some aspect of service-wide execution at the application level:

  • Use the ApplyDispatchBehavior method to change run-time property values or insert custom extension objects such as error handlers, message or parameter interceptors, security extensions, and other custom extension objects.

  • Use the Validate method to examine the description before Windows Communication Foundation (WCF) constructs the executing service to confirm that it can execute properly.

  • Use the AddBindingParameters method to pass to a binding element the custom information for the service so that it can support the service correctly.

IServiceBehavior objects can make use of any of these methods, but often only one is important; in such cases the unused methods can return without a value.

NoteNote:

All of the IServiceBehavior methods pass System.ServiceModel.Description.ServiceDescription and System.ServiceModel.ServiceHostBase objects as a parameters. The ServiceDescription parameter is for examination only; if you modify these objects the execution behavior is undefined.

To perform the customization task for which it is intended, the IServiceBehavior object must be added to the Behaviors property prior to the construction of the service runtime. There are three ways to do this:

Examples of service behaviors in WCF include the ServiceBehaviorAttribute attribute, the System.ServiceModel.Description.ServiceThrottlingBehavior, the System.ServiceModel.Description.ServiceDebugBehavior and the System.ServiceModel.Description.ServiceMetadataBehavior behavior.

The following code example shows the use of a service behavior specified in a configuration file to insert a custom error handler in a service application. In this example, the error handler catches all exceptions and converts them into a custom GreetingFault SOAP fault that is then returned to the client.

The following IServiceBehavior implementation adds no binding parameter objects, adds the custom System.ServiceModel.Dispatcher.IErrorHandler object to each ChannelDispatcher.ErrorHandlers property, and validates that each operation of the service to which the service behavior is applied and has a System.ServiceModel.FaultContractAttribute of type GreetingFault.

// This behavior modifies no binding parameters.
#region IServiceBehavior Members
public void AddBindingParameters(
  ServiceDescription description, 
  ServiceHostBase serviceHostBase, 
  System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, 
  System.ServiceModel.Channels.BindingParameterCollection parameters
)
{
  return;
}

// This behavior is an IErrorHandler implementation and  
// must be applied to each ChannelDispatcher. 
public void ApplyDispatchBehavior(ServiceDescription description, ServiceHostBase serviceHostBase)
{
  Console.WriteLine("The EnforceGreetingFaultBehavior has been applied.");
  foreach(ChannelDispatcher chanDisp in serviceHostBase.ChannelDispatchers)
  {
    chanDisp.ErrorHandlers.Add(this);      
  }
}

// This behavior requires that the contract have a SOAP fault with a detail type of GreetingFault. 
public void Validate(ServiceDescription description, ServiceHostBase serviceHostBase)
{
  Console.WriteLine("Validate is called.");
  foreach (ServiceEndpoint se in description.Endpoints)
  {
    // Must not examine any metadata endpoint. 
    if (se.Contract.Name.Equals("IMetadataExchange")
      && se.Contract.Namespace.Equals("http://schemas.microsoft.com/2006/04/mex"))
      continue;
    foreach (OperationDescription opDesc in se.Contract.Operations)
    {
      if (opDesc.Faults.Count == 0)
        throw new InvalidOperationException(String.Format(
          "EnforceGreetingFaultBehavior requires a "  
          + "FaultContractAttribute(typeof(GreetingFault)) in each operation contract.  "
          + "The \"{0}\" operation contains no FaultContractAttribute.",
          opDesc.Name)
        );
      bool gfExists = false;
      foreach (FaultDescription fault in opDesc.Faults)
      {
        if (fault.DetailType.Equals(typeof(GreetingFault)))
        {
          gfExists = true;
          continue;
        }
      }
      if (gfExists == false)
      {
        throw new InvalidOperationException(
"EnforceGreetingFaultBehavior requires a FaultContractAttribute(typeof(GreetingFault)) in an operation contract."
        );
      }
    }
  }
}
#endregion

In this example, the behavior class also implements System.ServiceModel.Configuration.BehaviorExtensionElement, which enables the service behavior to be inserted by using it in an application configuration file, as the following code example demonstrates.

<configuration>
  <system.serviceModel>
    <services>
      <service 
        name="Microsoft.WCF.Documentation.SampleService"
        behaviorConfiguration="metaAndErrors">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8080/SampleService"/>
          </baseAddresses>
        </host>
        <endpoint
          address=""
          binding="wsHttpBinding"
          contract="Microsoft.WCF.Documentation.ISampleService"
         />
        <endpoint
          address="mex"
          binding="mexHttpBinding"
          contract="IMetadataExchange"
         />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="metaAndErrors">
          <serviceDebug includeExceptionDetailInFaults="true"/>
          <serviceMetadata httpGetEnabled="true"/>
          <enforceGreetingFaults />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <extensions>
      <behaviorExtensions>
        <add 
          name="enforceGreetingFaults" 
          type="Microsoft.WCF.Documentation.EnforceGreetingFaultBehavior, HostApplication, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"
        />
      </behaviorExtensions>
    </extensions>
  </system.serviceModel>
</configuration>

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0

Community Additions

ADD
Show:
© 2014 Microsoft