IOperationBehavior Interface

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

Namespace:  System.ServiceModel.Description
Assembly:  System.ServiceModel (in System.ServiceModel.dll)

public interface IOperationBehavior

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.

NoteNote:

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 Dispatcher.

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.

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;
}

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; }

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft