Прочетете на английски Редактиране

Споделяне чрез


IContractBehaviorAttribute Interface

Definition

Specifies the contract for which an attribute that implements the IContractBehavior interface is active.

C#
public interface IContractBehaviorAttribute
Derived

Examples

The following code example assumes a custom System.ServiceModel.Dispatcher.IInstanceProvider implementation called ObjectProviderBehavior that provides a "singleton" behavior; it always returns the same service instance and does not recycle it.

To insert the instance provider customization, the example shows how to implement a custom attribute (SingletonBehaviorAttribute) that implements System.ServiceModel.Description.IContractBehavior to insert the custom service instance provider. It also implements IContractBehaviorAttribute, which binds its application to the ISampleService contract.

C#
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
}

Remarks

Implement the IContractBehaviorAttribute interface on an attribute that is also an System.ServiceModel.Description.IContractBehavior object to enable the use of that contract behavior attribute on a service type but restrict its application to the contract specified in the TargetContract property.

There is no requirement to implement IContractBehaviorAttribute on custom contract behavior attributes, and if the attribute is applied either to a contract interface or to a duplex callback class the value of the TargetContract property is ignored.

For more information, see System.ServiceModel.Description.IContractBehavior.

Properties

TargetContract

Gets the type of the contract to which the contract behavior is applicable.

Applies to

Продукт Версии
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1