IContractBehaviorAttribute.TargetContract 属性

定义

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

public:
 property Type ^ TargetContract { Type ^ get(); };
public Type TargetContract { get; }
member this.TargetContract : Type
Public ReadOnly Property TargetContract As Type

属性值

Type

适用于该协定行为的协定。

示例

下面的代码示例假定自定义 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

注解

实现 TargetContract 属性可以在将实现 System.ServiceModel.Description.IContractBehavior 属性应用到服务类时,指定该协定行为要应用到的协定。

适用于