IEndpointBehavior::ApplyDispatchBehavior Method (ServiceEndpoint^, EndpointDispatcher^)


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

Implements a modification or extension of the service across an endpoint.

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

void ApplyDispatchBehavior(
	ServiceEndpoint^ endpoint,
	EndpointDispatcher^ endpointDispatcher


Type: System.ServiceModel.Description::ServiceEndpoint^

The endpoint that exposes the contract.

Type: System.ServiceModel.Dispatcher::EndpointDispatcher^

The endpoint dispatcher to be modified or extended.

Implement the ApplyDispatchBehavior method to view, modify, or extend the service runtime across all messages or for specific operations in an endpoint. For details about what customizations you can do in a service application, see System.ServiceModel.Dispatcher::DispatchRuntime and System.ServiceModel.Dispatcher::DispatchOperation.

It is recommended that the ApplyDispatchBehavior method throw a NotImplementedException exception if the behavior is only intended for use in a client application.

Note that there can be two operations with the same name in the description when using a callback contract (one operation in each direction). If you are iterating through operations, you must correlate the message direction between the endpoint System.ServiceModel.Dispatcher::DispatchRuntime and what is returned by the DispatchRuntime::CallbackClientRuntime property.

In addition, because other behaviors may have already added or removed some operations from the runtime, there is no guarantee that there are the same number of operations in description as there are System.ServiceModel.Dispatcher::DispatchOperation objects in the DispatchRuntime::Operations property.

The following code example shows the implementation of an endpoint behavior that adds an System.ServiceModel.Dispatcher::IDispatchMessageInspector object in a service application. In this case, the EndpointBehaviorMessageInspector class implements System.ServiceModel.Dispatcher::IDispatchMessageInspector to inspect the inbound and outbound message, the IEndpointBehavior interface to insert the inspector class into the inspection system for all endpoints to which the behavior applies, and the System.ServiceModel.Configuration::BehaviorExtensionElement to enable the message inspector behavior using an application configuration file.

The first step is to implement the message inspector.

// IDispatchMessageInspector Members

public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, IClientChannel channel, InstanceContext instanceContext)
  Console.WriteLine("AfterReceiveRequest called.");
  return null;

public void BeforeSendReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
  Console.WriteLine("BeforeSendReply called.");

The next code example shows the use of the ApplyDispatchBehavior method to add the message inspector to the DispatchRuntime::MessageInspectors property.

// IEndpointBehavior Members
public void AddBindingParameters(ServiceEndpoint serviceEndpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)

public void ApplyClientBehavior(ServiceEndpoint serviceEndpoint, ClientRuntime behavior)
  behavior.MessageInspectors.Add(new EndpointBehaviorMessageInspector());

public void ApplyDispatchBehavior(ServiceEndpoint serviceEndpoint, EndpointDispatcher endpointDispatcher)
  endpointDispatcher.DispatchRuntime.MessageInspectors.Add(new EndpointBehaviorMessageInspector());

public void Validate(ServiceEndpoint serviceEndpoint)

The following code example shows the implementation of System.ServiceModel.Configuration::BehaviorExtensionElement in order to enable use of the message inspector behavior from a configuration file.

// BehaviorExtensionElement members
public override Type BehaviorType
  get { return typeof(EndpointBehaviorMessageInspector); }

protected override object CreateBehavior()
  return new EndpointBehaviorMessageInspector();

Finally, the following configuration file shows how the preceding example can be used from configuration.

Universal Windows Platform
Available since 8
.NET Framework
Available since 3.0
Portable Class Library
Supported in: portable .NET platforms
Available since 4.0
Return to top