編輯

共用方式為


IOperationBehavior Interface

Definition

Implements methods that can be used to extend run-time behavior for an operation in either a service or client application.

public interface class IOperationBehavior
public interface IOperationBehavior
type IOperationBehavior = interface
Public Interface IOperationBehavior
Derived

Examples

The following code example shows an implementation of System.ServiceModel.Dispatcher.IParameterInspector that writes to the console when the inspector is invoked on an operation. This customization can only be attached to the System.ServiceModel.Dispatcher.DispatchOperation or System.ServiceModel.Dispatcher.ClientOperation and is therefore usually inserted by an operation behavior.

#region IParameterInspector Members
public void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState)
{
  Console.WriteLine(
    "IParameterInspector.AfterCall called for {0} with return value {1}.",
    operationName,
    returnValue.ToString()
  );
}

public object BeforeCall(string operationName, object[] inputs)
{
  Console.WriteLine("IParameterInspector.BeforeCall called for {0}.", operationName);
  return null;
}
#Region "IParameterInspector Members"
    Public Sub AfterCall(ByVal operationName As String, ByVal outputs() As Object, ByVal returnValue As Object, _
                         ByVal correlationState As Object) Implements IParameterInspector.AfterCall
        Console.WriteLine("IParameterInspector.AfterCall called for {0} with return value {1}.", _
                          operationName, returnValue.ToString())
    End Sub

    Public Function BeforeCall(ByVal operationName As String, ByVal inputs() As Object) As Object Implements _
    IParameterInspector.BeforeCall
        Console.WriteLine("IParameterInspector.BeforeCall called for {0}.", operationName)
        Return Nothing
    End Function

The following code example shows how the operation behavior attaches the parameter inspector to the runtime.

#region IOperationBehavior Members
public void AddBindingParameters(
  OperationDescription operationDescription, BindingParameterCollection bindingParameters
)
{ return; }

public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation)
{
  clientOperation.ParameterInspectors.Add(new Inspector());
}

public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)
{
  dispatchOperation.ParameterInspectors.Add(new Inspector());
}

public void Validate(OperationDescription operationDescription){ return; }
#Region "IOperationBehavior Members"
    Public Sub AddBindingParameters(ByVal operationDescription As OperationDescription, _
                                    ByVal bindingParameters As BindingParameterCollection) Implements _
                                    IOperationBehavior.AddBindingParameters
        Return
    End Sub

    Public Sub ApplyClientBehavior(ByVal operationDescription As OperationDescription, ByVal _
                                   clientOperation As ClientOperation) Implements IOperationBehavior.ApplyClientBehavior
        clientOperation.ParameterInspectors.Add(New Inspector())
    End Sub

    Public Sub ApplyDispatchBehavior(ByVal operationDescription As OperationDescription, ByVal dispatchOperation As  _
                                     DispatchOperation) Implements IOperationBehavior.ApplyDispatchBehavior
        dispatchOperation.ParameterInspectors.Add(New Inspector())
    End Sub

Public Sub Validate(ByVal operationDescription As OperationDescription) Implements IOperationBehavior.Validate
    Return
End Sub

Remarks

Implement the IOperationBehavior interface to modify, examine, or extend some aspect of operation-wide execution at the application level for either client or service applications.

  • Use the AddBindingParameters method to pass custom data at runtime to enable bindings to support custom behavior.

  • Use the ApplyClientBehavior method to modify, examine, or insert extensions to a client dispatcher in a client application.

  • Use the ApplyDispatchBehavior method to modify, examine, or insert extensions to operation-wide execution in a service application.

  • Use the Validate method to confirm that a OperationDescription meets specific requirements. This can be used to ensure that an operation has a certain configuration setting enabled, supports a particular feature and other requirements.

IOperationBehavior objects can make use of any of these methods, but often only one is important; in such cases, the unused methods can return, performing no action.

Note

All of the IOperationBehavior methods pass an OperationDescription object as a parameter. This parameter is for examination only; if you modify the OperationDescription object the execution behavior is undefined.

IOperationBehavior objects are typically used to access the various properties of the System.ServiceModel.Dispatcher.DispatchOperation object in a service application and the System.ServiceModel.Dispatcher.ClientOperation object in a client application.

Typically, the developer first reviews the extensibility points to determine which customization option suits the application scenario and then implements the customization at the appropriate scope. For example, System.ServiceModel.Description.IServiceBehavior objects can insert customizations for all messages in an entire service and System.ServiceModel.Description.IContractBehavior objects can insert customizations for all messages across a specific contract, and so on. For a description of the various properties and customizations available, see Extending ServiceHost and the Service Model Layer.

Once a customization has been decided upon (and the customization interface implemented if necessary) and the IOperationBehavior is the appropriate scope of customization, the customization must be inserted into the Windows Communication Foundation (WCF) runtime by implementing IOperationBehavior and adding the operation behavior to the runtime.

There are two ways to add the IOperationBehavior to the runtime:

  • Programmatically add the custom operation behavior to the OperationDescription.Behaviors property prior to the opening of the service host (in a service application) or the channel factory (in a client application).

  • Add the behavior using a custom attribute.

To perform the service customization task for which it is intended, the IOperationBehavior object must be added to the OperationDescription.Behaviors property prior to the construction of the service runtime, which occurs when ICommunicationObject.Open method is called on System.ServiceModel.ServiceHost. To perform a client customization task, the IOperationBehavior object must be added to the OperationDescription.Behaviors property before calling the ChannelFactory<TChannel>.CreateChannel method or the ICommunicationObject.Open method on ChannelFactory<TChannel>.

Although the operation behavior is designed for easy access to the runtime at the scope of an individual operation, you can access the runtime at a larger scope by accessing the parent runtime object.

Methods

AddBindingParameters(OperationDescription, BindingParameterCollection)

Implement to pass data at runtime to bindings to support custom behavior.

ApplyClientBehavior(OperationDescription, ClientOperation)

Implements a modification or extension of the client across an operation.

ApplyDispatchBehavior(OperationDescription, DispatchOperation)

Implements a modification or extension of the service across an operation.

Validate(OperationDescription)

Implement to confirm that the operation meets some intended criteria.

Applies to