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 物件的屬性 (Attribute) 上實作 System.ServiceModel.Description.IContractBehavior 介面,以在服務型別上啟用該合約行為屬性 (Attribute),但將其應用程式限制為在 TargetContract 屬性 (Property) 中指定的合約。

在自訂合約行為屬性 (Attribute) 上實作 IContractBehaviorAttribute 並沒有需求,且如果屬性 (Attribute) 套用至合約介面或雙工回呼類別,便會忽略 TargetContract 屬性 (Property) 的值。

如需詳細資訊,請參閱System.ServiceModel.Description.IContractBehavior

屬性

TargetContract

取得合約行為適用的合約的型別。

適用於