Export (0) Print
Expand All
1 out of 2 rated this helpful - Rate this topic

IEndpointBehavior Interface

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

Namespace:  System.ServiceModel.Description
Assembly:  System.ServiceModel (in System.ServiceModel.dll)
'Declaration
Public Interface IEndpointBehavior

The IEndpointBehavior type exposes the following members.

  NameDescription
Public methodSupported in .NET for Windows Store appsAddBindingParametersImplement to pass data at runtime to bindings to support custom behavior.
Public methodSupported in .NET for Windows Store appsApplyClientBehaviorImplements a modification or extension of the client across an endpoint.
Public methodSupported in .NET for Windows Store appsApplyDispatchBehaviorImplements a modification or extension of the service across an endpoint.
Public methodSupported in .NET for Windows Store appsValidateImplement to confirm that the endpoint meets some intended criteria.
Top

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.

NoteNote

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(Of TChannel).CreateChannel method or the ICommunicationObject.Open method on ChannelFactory(Of TChannel).

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>
<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>

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

.NET for Windows Store apps

Supported in: Windows 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.