IContractBehaviorAttribute Interfejs

Definicja

Określa kontrakt, dla którego atrybut implementujący IContractBehavior interfejs jest aktywny.

public interface class IContractBehaviorAttribute
public interface IContractBehaviorAttribute
type IContractBehaviorAttribute = interface
Public Interface IContractBehaviorAttribute
Pochodne

Przykłady

W poniższym przykładzie kodu przyjęto założenie, że implementacja niestandardowa System.ServiceModel.Dispatcher.IInstanceProvider o nazwie ObjectProviderBehavior zapewnia zachowanie "pojedynczego" — zawsze zwraca to samo wystąpienie usługi i nie przetwarza go.

Aby wstawić dostosowanie dostawcy wystąpień, w przykładzie pokazano, jak zaimplementować atrybut niestandardowy (SingletonBehaviorAttribute), który implementuje System.ServiceModel.Description.IContractBehavior wstawianie niestandardowego dostawcy wystąpienia usługi. Implementuje IContractBehaviorAttributerównież element , który wiąże swoją aplikację z kontraktem 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

Uwagi

Zaimplementuj interfejs na atrybucie IContractBehaviorAttribute , który jest również obiektem System.ServiceModel.Description.IContractBehavior , aby umożliwić użycie tego atrybutu zachowania kontraktu w typie usługi, ale ograniczyć jego aplikację do kontraktu określonego TargetContract we właściwości.

Nie ma potrzeby implementowania IContractBehaviorAttribute atrybutów niestandardowego zachowania kontraktu, a jeśli atrybut jest stosowany do interfejsu kontraktu lub do klasy wywołania zwrotnego dwukierunkowego wartość TargetContract właściwości jest ignorowana.

Aby uzyskać więcej informacji, zobacz System.ServiceModel.Description.IContractBehavior.

Właściwości

TargetContract

Pobiera typ kontraktu, do którego ma zastosowanie zachowanie kontraktu.

Dotyczy