IEndpointBehavior Interface

Definition

Implements methods that can be used to extend run-time behavior for an endpoint in either a service or client application.

public interface class IEndpointBehavior
public interface IEndpointBehavior
type IEndpointBehavior = interface
Public Interface IEndpointBehavior
Derived

Examples

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)
{
  return;
}

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)
{
  return;
}

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.

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

Remarks

Implement the IEndpointBehavior interface to modify, examine, or extend some aspect of endpoint-wide execution at the application level for either client or service applications.

  • Use the AddBindingParameters method to pass custom data at runtime to enable bindings to support custom behavior.

  • Use the ApplyClientBehavior method to modify, examine, or insert extensions to an endpoint in a client application.

  • Use the ApplyDispatchBehavior method to modify, examine, or insert extensions to endpoint-wide execution in a service application.

  • Use the Validate method to confirm that a ServiceEndpoint meets specific requirements. This can be used to ensure that an endpoint has a certain configuration setting enabled, supports a particular feature and other requirements.

IEndpointBehavior objects can make use of any of these methods, but often only one is important; in such cases, the unused methods can return, performing no action.

Note

All of the IEndpointBehavior methods pass a ServiceEndpoint object as a parameter. This parameter is for examination only; if you modify the ServiceEndpoint object the execution behavior is undefined.

IEndpointBehavior objects are typically used to access the various properties of the System.ServiceModel.Dispatcher.DispatchRuntime, System.ServiceModel.Dispatcher.DispatchOperation, System.ServiceModel.Dispatcher.EndpointDispatcher, and System.ServiceModel.Dispatcher.ChannelDispatcher objects in a service application and the System.ServiceModel.Dispatcher.ClientRuntime and System.ServiceModel.Dispatcher.ClientOperation in a client application. In addition, you can access the properties of duplex clients and services using the ClientRuntime.CallbackDispatchRuntime and DispatchRuntime.CallbackClientRuntime properties, respectively.

For a description of the various properties and customizations available, see Extending ServiceHost and the Service Model Layer.

Once a customization has been decided upon (and the customization interface implemented if necessary) and the IEndpointBehavior has been decided is the appropriate scope of customization, the customization must be inserted into the Windows Communication Foundation (WCF) runtime by implementing IEndpointBehavior and adding the endpoint behavior to the runtime.

There are two ways to add the behavior to the runtime:

  • Programmatically add the custom endpoint behavior to the Behaviors property prior to the opening of the service host (in a service application) or the channel factory (in a client application).

  • Configure the behavior using an application configuration file. For details, see <behaviorExtensions>.

To perform the service customization task for which it is intended, the IEndpointBehavior object must be added to the ServiceEndpoint.Behaviors property prior to the construction of the service runtime, which occurs when ICommunicationObject.Open method is called on System.ServiceModel.ServiceHost. To perform a client customization task, the IEndpointBehavior object must be added to the ServiceEndpoint.Behaviors property before calling the ChannelFactory<TChannel>.CreateChannel method or the ICommunicationObject.Open method on ChannelFactory<TChannel>.

Methods

AddBindingParameters(ServiceEndpoint, BindingParameterCollection)

Implement to pass data at runtime to bindings to support custom behavior.

ApplyClientBehavior(ServiceEndpoint, ClientRuntime)

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

ApplyDispatchBehavior(ServiceEndpoint, EndpointDispatcher)

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

Validate(ServiceEndpoint)

Implement to confirm that the endpoint meets some intended criteria.

Applies to