IContractBehavior Interface
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Implements methods that can be used to extend run-time behavior for a contract in either a service or client application.
public interface class IContractBehavior
public interface IContractBehavior
type IContractBehavior = interface
Public Interface IContractBehavior
- Derived
Examples
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
}
Public Class SingletonBehaviorAttribute
Inherits Attribute
Implements IContractBehaviorAttribute, IContractBehavior
#Region "IContractBehaviorAttribute Members"
Public ReadOnly Property TargetContract() As Type Implements IContractBehaviorAttribute.TargetContract
Get
Return GetType(ISampleService)
End Get
End Property
#End Region
#Region "IContractBehavior Members"
Public Sub AddBindingParameters(ByVal description As ContractDescription, ByVal endpoint As ServiceEndpoint, ByVal parameters As System.ServiceModel.Channels.BindingParameterCollection) Implements IContractBehavior.AddBindingParameters
Return
End Sub
Public Sub ApplyClientBehavior(ByVal description As ContractDescription, ByVal endpoint As ServiceEndpoint, ByVal clientRuntime As ClientRuntime) Implements IContractBehavior.ApplyClientBehavior
Return
End Sub
Public Sub ApplyDispatchBehavior(ByVal description As ContractDescription, ByVal endpoint As ServiceEndpoint, ByVal dispatch As DispatchRuntime) Implements IContractBehavior.ApplyDispatchBehavior
dispatch.InstanceProvider = New ObjectProviderBehavior("Custom ObjectProviderBehavior constructor.")
End Sub
Public Sub Validate(ByVal description As ContractDescription, ByVal endpoint As ServiceEndpoint) Implements IContractBehavior.Validate
Return
End Sub
#End Region
End Class
Remarks
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.
Note
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.
Note
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:
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 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:
Programmatically add the custom contract behavior to the Behaviors property prior to the point when the ChannelFactory<TChannel>.CreateChannel is called.
Create a custom attribute that also implements IContractBehavior.
For more information on programmatically adding IContractBehavior types to either the client or service application, see Configuring and Extending the Runtime with Behaviors.
Methods
AddBindingParameters(ContractDescription, ServiceEndpoint, BindingParameterCollection) |
Configures any binding elements to support the contract behavior. |
ApplyClientBehavior(ContractDescription, ServiceEndpoint, ClientRuntime) |
Implements a modification or extension of the client across a contract. |
ApplyDispatchBehavior(ContractDescription, ServiceEndpoint, DispatchRuntime) |
Implements a modification or extension of the client across a contract. |
Validate(ContractDescription, ServiceEndpoint) |
Implement to confirm that the contract and endpoint can support the contract behavior. |