IServiceBehavior Interface


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

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

System_CAPS_pubmethodAddBindingParameters(ServiceDescription, ServiceHostBase, Collection(Of ServiceEndpoint), BindingParameterCollection)

Provides the ability to pass custom data to binding elements to support the contract implementation.

System_CAPS_pubmethodApplyDispatchBehavior(ServiceDescription, ServiceHostBase)

Provides the ability 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.

System_CAPS_pubmethodValidate(ServiceDescription, ServiceHostBase)

Provides the ability to inspect the service host and the service description to confirm that the service can run successfully.

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.


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 Sub AddBindingParameters(ByVal description As ServiceDescription, ByVal serviceHostBase As ServiceHostBase, ByVal endpoints As System.Collections.ObjectModel.Collection(Of ServiceEndpoint), ByVal parameters As System.ServiceModel.Channels.BindingParameterCollection) Implements IServiceBehavior.AddBindingParameters
End Sub

' This behavior is an IErrorHandler implementation and 
' must be applied to each ChannelDispatcher.
Public Sub ApplyDispatchBehavior(ByVal description As ServiceDescription, ByVal serviceHostBase As ServiceHostBase) Implements IServiceBehavior.ApplyDispatchBehavior
  Console.WriteLine("The EnforceGreetingFaultBehavior has been applied.")
  For Each chanDisp As ChannelDispatcher In serviceHostBase.ChannelDispatchers
  Next chanDisp
End Sub

' This behavior requires that the contract have a SOAP fault with a detail type of GreetingFault.
Public Sub Validate(ByVal description As ServiceDescription, ByVal serviceHostBase As ServiceHostBase) Implements IServiceBehavior.Validate
  Console.WriteLine("Validate is called.")
  For Each se As ServiceEndpoint In description.Endpoints
	' Must not examine any metadata endpoint.
	If se.Contract.Name.Equals("IMetadataExchange") AndAlso se.Contract.Namespace.Equals("") Then
	  Continue For
	End If
	For Each opDesc As OperationDescription In se.Contract.Operations
	  If opDesc.Faults.Count = 0 Then
		Throw New InvalidOperationException(String.Format("EnforceGreetingFaultBehavior requires a " & "FaultContractAttribute(typeof(GreetingFault)) in each operation contract.  " & "The ""{0}"" operation contains no FaultContractAttribute.", opDesc.Name))
	  End If
	  Dim gfExists As Boolean = False
	  For Each fault As FaultDescription In opDesc.Faults
		If fault.DetailType.Equals(GetType(GreetingFault)) Then
		  gfExists = True
		  Continue For
		End If
	  Next fault
	  If gfExists = False Then
		Throw New InvalidOperationException("EnforceGreetingFaultBehavior requires a FaultContractAttribute(typeof(GreetingFault)) in an operation contract.")
	  End If
	Next opDesc
  Next se
End Sub
#End Region

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.

.NET Framework
Available since 3.0
Return to top