IContractBehaviorAttribute Interface


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Specifies the contract for which an attribute that implements the System.ServiceModel.Description.IContractBehavior interface is active.

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

Public Interface IContractBehaviorAttribute


Gets the type of the contract to which the contract behavior is applicable.

Implement the IContractBehaviorAttribute interface on an attribute that is also an System.ServiceModel.Description.IContractBehavior object to enable the use of that contract behavior attribute on a service type but restrict its application to the contract specified in the TargetContract property.

There is no requirement to implement IContractBehaviorAttribute on custom contract behavior attributes, and if the attribute is applied either to a contract interface or to a duplex callback class the value of the TargetContract property is ignored.

For more information, see System.ServiceModel.Description.IContractBehavior.

The following code example assumes a custom System.ServiceModel.Dispatcher.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 System.ServiceModel.Description.IContractBehavior to insert the custom service instance provider. It also implements IContractBehaviorAttribute, which binds its application to the ISampleService contract.

 Public Class SingletonBehaviorAttribute
  Inherits Attribute
  Implements IContractBehaviorAttribute, IContractBehavior

#Region "IContractBehaviorAttribute Members"

Public ReadOnly Property TargetContract() As Type Implements IContractBehaviorAttribute.TargetContract
	  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
End Sub

Public Sub ApplyClientBehavior(ByVal description As ContractDescription, ByVal endpoint As ServiceEndpoint, ByVal clientRuntime As ClientRuntime) Implements IContractBehavior.ApplyClientBehavior
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
End Sub

#End Region
 End Class

.NET Framework
Available since 3.0
Return to top