IContractBehaviorAttribute 接口

定义

指定其实现 IContractBehavior 接口的属性处于活动状态的协定。

public interface class IContractBehaviorAttribute
public interface IContractBehaviorAttribute
type IContractBehaviorAttribute = interface
Public Interface IContractBehaviorAttribute
派生

示例

下面的代码示例假定自定义 System.ServiceModel.Dispatcher.IInstanceProvider 实现调用了可提供“单一实例”行为的 ObjectProviderBehavior,它始终返回同一服务实例,且不回收该服务实例。

对于实例提供程序自定义的插入,该示例演示了如何实现一个自定义属性 (SingletonBehaviorAttribute),该自定义属性可实现 System.ServiceModel.Description.IContractBehavior,从而插入自定义服务实例提供程序。 它还实现了 IContractBehaviorAttribute,这会将其应用程序绑定到 ISampleService 协定。

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

注解

对一个同时为 IContractBehaviorAttribute 对象的属性实现 System.ServiceModel.Description.IContractBehavior 接口可以对服务类型使用该协定行为属性,但会将此属性的使用限定为 TargetContract 属性中指定的协定。

不要求在自定义协定行为属性上实现 IContractBehaviorAttribute,如果该属性应用到协定接口或双向回调类,则将忽略 TargetContract 属性的值。

有关详细信息,请参阅 System.ServiceModel.Description.IContractBehavior

属性

TargetContract

获取可用该协定行为的协定的类型。

适用于