共用方式為


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 屬性 (Property),以指定當實作的 System.ServiceModel.Description.IContractBehavior 屬性 (Attribute) 套用至服務類別時,套用合約行為的合約。

適用於