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

The IContractBehavior type exposes the following members.

  NameDescription
Public methodSupported in .NET for Windows Store appsAddBindingParametersConfigures any binding elements to support the contract behavior.
Public methodSupported in .NET for Windows Store appsApplyClientBehaviorImplements a modification or extension of the client across a contract.
Public methodSupported in .NET for Windows Store appsApplyDispatchBehaviorImplements a modification or extension of the client across a contract.
Public methodSupported in .NET for Windows Store appsValidateImplement to confirm that the contract and endpoint can support the contract behavior.
Top

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 Configuring and Extending the Runtime with 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 Configuring and Extending the Runtime with 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.

No code example is currently available or this language may not be supported.

.NET Framework

Supported in: 4.5.2, 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 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.

Show:
© 2014 Microsoft