Implements methods that can be used to extend run-time behavior for a contract in either a service or client application.
Assembly: System.ServiceModel (in System.ServiceModel.dll)
Thetype exposes the following members.
|AddBindingParameters||Configures any binding elements to support the contract behavior.|
|ApplyClientBehavior||Implements a modification or extension of the client across a contract.|
|ApplyDispatchBehavior||Implements a modification or extension of the client across a contract.|
|Validate||Implement to confirm that the contract and endpoint can support the contract behavior.|
Implement the interface to modify, examine, or extend some aspect of contract-wide execution at the application level. Unlike IServiceBehavior and IEndpointBehavior objects, 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.
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.
All of the 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.
types can be used on either the service or the client, or both. To perform a customization task on the service, the 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.
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 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:
A contract interface. In this case, the behavior is applied to all contracts of that type in any endpoint and Windows Communication Foundation (WCF) ignores the value of the IContractBehaviorAttribute.TargetContract property.
A service class. In this case, the behavior is applied only to endpoints the contract of which is the value of the IContractBehaviorAttribute.TargetContract property.
A callback class. In this case, the behavior is applied to the duplex client's endpoint and WCF ignores the value of the IContractBehaviorAttribute.TargetContract property.
To perform the customization task on the client for which it is intended, the 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:
Create a custom attribute that also implements .
For more information on programmatically adding 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 to insert the custom service instance provider. It also implements IContractBehaviorAttribute, which binds its use to the ISampleService contract.
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)