Export (0) Print
Expand All

IContractBehavior Interface

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

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

public interface IContractBehavior

Implement the IContractBehavior interface to modify, examine, or extend some aspect of contract-wide execution at the application level. Unlike IServiceBehavior and IEndpointBehavior objects, IContractBehavior objects cannot be added to the runtime using an application configuration file; they can only be added programmatically or using an attribute.

For more information about choosing between service, endpoint, and contract behaviors, see Attaching Extensions Using Behaviors.

  • Use the AddBindingParameters method to provide binding elements with custom data to support the behavior.

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

  • Use the ApplyDispatchBehavior method to modify, examine, or insert extensions to a contract in a service application.

  • Use the Validate method to ensure that a contract can support a particular feature.

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

NoteNote:

All of the IContractBehavior methods pass System.ServiceModel.Description.ContractDescription and System.ServiceModel.Description.ServiceEndpoint as parameters. These parameters are for examination; if you modify the objects the execution behavior is undefined.

IContractBehavior types can be used on either the service or the client, or both. To perform a customization task on the service, the IContractBehavior object must be added to the Behaviors property prior to the construction of the service runtime, which occurs when the ICommunicationObject.Open method is called on the System.ServiceModel.ServiceHost object. There are two ways to do this.

The first method is to programmatically add the custom contract behavior to the Behaviors property prior to the point when the ICommunicationObject.Open method is called on the System.ServiceModel.ServiceHost object. When applied this way, the behavior is applied for all messages flowing through that contract on any endpoint.

NoteNote:

The behavior is applied to all contracts of the same type. For example, if you programmatically add the same contract type to more than one endpoint, the behavior modifies all endpoints that refer to the same contract object.

The second method is to create a custom attribute that implements IContractBehavior and apply that to:

  • A contract interface. In this case, the behavior is applied to all contracts of that type in any endpoint.

  • A service class. In this case, the behavior is applied to all endpoints regardless of contract.

  • A callback class. In this case, the behavior is applied to the duplex client's endpoint.

The behavior of the second approach varies slightly if the custom attribute also implements System.ServiceModel.Description.IContractBehaviorAttribute. In this case, the behavior is as follows:

To perform the customization task on the client for which it is intended, the IContractBehavior object must be added to the Behaviors property prior to the construction of the client runtime, which occurs when ChannelFactory<TChannel>.CreateChannel is called. There are two ways to do this:

For more information on programmatically adding IContractBehavior types to either the client or service application, see Attaching Extensions Using Behaviors.

The following code example assumes a custom IInstanceProvider implementation called ObjectProviderBehavior that provides a "singleton" behavior; it always returns the same service instance and does not recycle it.

To insert the instance provider customization, the example shows how to implement a custom attribute (SingletonBehaviorAttribute) that implements IContractBehavior to insert the custom service instance provider. It also implements IContractBehaviorAttribute, which binds its use to the ISampleService contract.

public class SingletonBehaviorAttribute : Attribute, IContractBehaviorAttribute, IContractBehavior
{

  #region IContractBehaviorAttribute Members

  public Type TargetContract
  {
    get { return typeof(ISampleService); }
  }

  #endregion

  #region IContractBehavior Members

  public void AddBindingParameters(ContractDescription description, ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection parameters)
  {
    return;
  }

  public void ApplyClientBehavior(ContractDescription description, ServiceEndpoint endpoint, ClientRuntime clientRuntime)
  {
    return;
  }

  public void ApplyDispatchBehavior(ContractDescription description, ServiceEndpoint endpoint, DispatchRuntime dispatch)
  {
    dispatch.InstanceProvider = new ObjectProviderBehavior("Custom ObjectProviderBehavior constructor.");
  }

  public void Validate(ContractDescription description, ServiceEndpoint endpoint)
  {
    return;
  }

  #endregion
}

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