Compartilhar via


WCF Extensibility - IOperationBehavior

This post is part of a series about WCF extensibility points. For a list of all previous posts and planned future ones, go to the index page .

The last of the behavior interfaces in WCF is the IOperationBehavior. The scope of this one is limited to an individual operation, so one can have different operations in the same contract. For example, the OperationBehaviorAttribute can define that some specific operations in the contract will auto-complete transactions (if the operation returns successfully, i.e., does not throw an exception). Operation behaviors are typically used for those scenarios, when one wants fine-grained control over the operations in a service contract.

Public implementations in WCF

 

Interface declaration

  1. public interface IOperationBehavior
  2. {
  3.     void AddBindingParameters(OperationDescription operationDescription, BindingParameterCollection bindingParameters);
  4.     void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation);
  5.     void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation);
  6.     void Validate(OperationDescription operationDescription);
  7. }

Again, like in the previous behavior interfaces, Validate is called first, with the operation description passed as a parameter. If the behavior finds the description violating some of its logic, it should throw an exception to abort the host (or the client) from opening. AddBindingParameters is called next, and again this is not commonly used.

ApplyDispatchBehavior and ApplyClientBehavior are called last, and they receive the operation description and the operation runtime (DispatchOperation for the server side, ClientOperation for client side). Among the properties in the operation runtime which are commonly used are the list of parameter inspectors (client / server) and the message formatter (client / server), which will be covered later in this series.

How to add an operation behavior

Via service / endpoint / contract description (server) : You can obtain a reference to the operation description via the contract description, and from there you can get a reference to the list of operation behaviors:

  1. string baseAddress = "https://" + Environment.MachineName + ":8000/Service";
  2. ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
  3. ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof(ITest), new WSHttpBinding(), "");
  4. foreach (OperationDescription od in endpoint.Contract.Operations)
  5. {
  6.     od.Behaviors.Add(new MyOperationBehavior());
  7. }

Via channel factory / generated proxy (client) : Like on the server side, once you get a reference to the endpoint, you can reference the contract, then the operation description collection. Notice that (just like in the service) you don’t need to enumerate all the operations, you can select by name to apply the behavior to specific operations.

  1. ChannelFactory<ITest> factory = new ChannelFactory<ITest>(binding, new EndpointAddress(address));
  2. factory.Endpoint.Contract.Operations.Find("Add").Behaviors.Add(new MyOperationBehavior());

Via attributes: If the operation behavior is derived from System.Attribute, it can be applied to the operation, either at the contract interface or at the actual implementation of the operation in the service code, and it will be added to the operation description by WCF. Adding it to both places is the same as adding it to a single one (the list of behaviors is a dictionary keyed by the behavior type, so a behavior collection cannot have more than one instance of each type).

  1. public class MyOperationBehaviorAttribute : Attribute, IOperationBehavior
  2. {
  3.     // IOperationBehavior implementation
  4. }
  5. [ServiceContract]
  6. public interface ICalculator
  7. {
  8.     [OperationContract]
  9.     [MyOperationBehavior]
  10.     int Add(int x, int y);
  11.     [OperationContract]
  12.     int Multiply(int x, int y);
  13. }
  14. public class CalculatorService : ICalculator
  15. {
  16.     public int Add(int x, int y)
  17.     {
  18.         return x + y;
  19.     }
  20.  
  21.     [MyOperationBehavior]
  22.     public int Multiply(int x, int y)
  23.     {
  24.         return x * y;
  25.     }
  26. }

Using configuration: Configuration is not an option for adding operation behaviors.

Real world scenario: a better parameter inspector

The IParameterInspector interface is a great way to analyze the inputs and outputs to operations in WCF services – it provides a callback where one can look, log, change, etc. the input and output parameters. It also lets the users see the return value of the operation, but doesn’t let the inspector change it. A few posts from the forums needed to actually change the return value as well. In one of them, the user Antonello Tesoro offered a great, simple suggestion on how to implement such inspector (wrapping an operation invoker), and I’ll implement his idea to show a real usage of operation behaviors.

And here goes the usual disclaimer – this is a sample for illustrating the topic of this post, this is not production-ready code. I tested it for a few contracts and it worked, but I cannot guarantee that it will work for all scenarios (please let me know if you find a bug or something missing). Also, for simplicity sake it doesn’t have a lot of error handling which a production-level code would, and doesn’t support all contract types (asynchronous operations, for example, are only partially supported). Finally, this sample (as well as most other samples in this series) uses extensibility points other than the one for this post (e.g., operation invoker) which are necessary to get a realistic scenario going. I’ll briefly describe what they do, and leave to their specific entries a more detailed description of their behavior.

First, let’s define the new inspector interface. It’s almost a copy of IParameterInspector, with the only difference being the returnValue parameter to AfterCall being passed as reference:

  1. public interface IBetterParameterInspector
  2. {
  3.     object BeforeCall(string operationName, object[] inputs);
  4.     void AfterCall(string operationName, object[] outputs, ref object returnValue, object correlationState);
  5. }

On to the behavior. On the call to ApplyDispatchBehavior, the DispatchOperation object is already created and populated. We’ll then replace the existing operation invoker with our own implementation, passing the existing one to it:

  1. public class BetterParameterInspectorOperationBehavior : IOperationBehavior
  2. {
  3.     IBetterParameterInspector inspector;
  4.     public BetterParameterInspectorOperationBehavior(IBetterParameterInspector inspector)
  5.     {
  6.         if (inspector == null)
  7.         {
  8.             throw new ArgumentNullException("inspector");
  9.         }
  10.  
  11.         this.inspector = inspector;
  12.     }
  13.  
  14.     public void AddBindingParameters(OperationDescription operationDescription, BindingParameterCollection bindingParameters)
  15.     {
  16.     }
  17.  
  18.     public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation)
  19.     {
  20.     }
  21.  
  22.     public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)
  23.     {
  24.         dispatchOperation.Invoker = new BetterParameterInspectorOperationInvoker(operationDescription.Name, dispatchOperation.Invoker, this.inspector);
  25.     }
  26.  
  27.     public void Validate(OperationDescription operationDescription)
  28.     {
  29.     }
  30. }

The new operation invoker will simply delegate all the calls to the original invoker, calling the inspector before and after completing each call. On synchronous method implementations, the implementation of the correlation state is trivial – simply store the return value of BeforeCall in a local variable, invoke the operation, then use that variable as the correlationState parameter to AfterCall. On asynchronous operations, however, it isn’t as simple. For this sample, the code throws if an inspector to an asynchronous operation returns anything other than null on BeforeCall. To fully implement correlation state for such operations, we’d need to chain the asynchronous operation as described in https://blogs.msdn.com/b/carlosfigueira/archive/2009/07/23/chaining-async-calls-in-wcf-or-in-net-in-general.aspx – too complex for this sample.

  1. class BetterParameterInspectorOperationInvoker : IOperationInvoker
  2. {
  3.     string operationName;
  4.     IOperationInvoker originalInvoker;
  5.     IBetterParameterInspector inspector;
  6.     public BetterParameterInspectorOperationInvoker(string operationName, IOperationInvoker originalInvoker, IBetterParameterInspector inspector)
  7.     {
  8.         this.operationName = operationName;
  9.         this.originalInvoker = originalInvoker;
  10.         this.inspector = inspector;
  11.     }
  12.  
  13.     public object[] AllocateInputs()
  14.     {
  15.         return this.originalInvoker.AllocateInputs();
  16.     }
  17.  
  18.     public object Invoke(object instance, object[] inputs, out object[] outputs)
  19.     {
  20.         object correlationState = this.inspector.BeforeCall(this.operationName, inputs);
  21.         object result = this.originalInvoker.Invoke(instance, inputs, out outputs);
  22.         this.inspector.AfterCall(this.operationName, outputs, ref result, correlationState);
  23.         return result;
  24.     }
  25.  
  26.     public IAsyncResult InvokeBegin(object instance, object[] inputs, AsyncCallback callback, object state)
  27.     {
  28.         object correlationState = this.inspector.BeforeCall(this.operationName, inputs);
  29.         if (correlationState != null)
  30.         {
  31.             throw new InvalidOperationException("Correlation not implemented yet for asynchronous methods");
  32.         }
  33.  
  34.         return this.originalInvoker.InvokeBegin(instance, inputs, callback, state);
  35.     }
  36.  
  37.     public object InvokeEnd(object instance, out object[] outputs, IAsyncResult result)
  38.     {
  39.         object operationResult = this.originalInvoker.InvokeEnd(instance, out outputs, result);
  40.         this.inspector.AfterCall(this.operationName, outputs, ref operationResult, null);
  41.         return operationResult;
  42.     }
  43.  
  44.     public bool IsSynchronous
  45.     {
  46.         get { return this.originalInvoker.IsSynchronous; }
  47.     }
  48. }

And that’s it. Now let’s put them to use. To test this scenario, I’ll define two service contract interfaces, one with synchronous operations, one with asynchronous ones.

  1. [ServiceContract]
  2. public interface ITest
  3. {
  4.     [OperationContract]
  5.     int Add(int x, int y);
  6.     [OperationContract]
  7.     int Subtract(int x, int y);
  8.     [OperationContract]
  9.     int Divide(int x, int y);
  10.     [OperationContract]
  11.     int Multiply(int x, int y);
  12. }
  13.  
  14. [ServiceContract]
  15. public interface ITestWithAsyncOperation
  16. {
  17.     [OperationContract(AsyncPattern = true)]
  18.     IAsyncResult BeginEchoString(string text, AsyncCallback callback, object userState);
  19.     string EndEchoString(IAsyncResult asyncResult);
  20. }

Their implementations are trivial (for the asynchronous one we delegate to the Func<T,TResult> delegate and let it handle the asynchronous behavior for us).

  1. public class Service : ITest, ITestWithAsyncOperation
  2. {
  3.     public int Add(int x, int y)
  4.     {
  5.         int result = x + y;
  6.         Console.WriteLine("{0} + {1} = {2}", x, y, result);
  7.         return result;
  8.     }
  9.  
  10.     public int Subtract(int x, int y)
  11.     {
  12.         int result = x - y;
  13.         Console.WriteLine("{0} - {1} = {2}", x, y, result);
  14.         return result;
  15.     }
  16.  
  17.     public int Divide(int x, int y)
  18.     {
  19.         int result = x / y;
  20.         Console.WriteLine("{0} / {1} = {2}", x, y, result);
  21.         return result;
  22.     }
  23.  
  24.     public int Multiply(int x, int y)
  25.     {
  26.         int result = x * y;
  27.         Console.WriteLine("{0} * {1} = {2}", x, y, result);
  28.         return result;
  29.     }
  30.  
  31.     public IAsyncResult BeginEchoString(string text, AsyncCallback callback, object userState)
  32.     {
  33.         Func<string, string> func = this.EchoStringDoWork;
  34.         return func.BeginInvoke(text, callback, userState);
  35.     }
  36.  
  37.     public string EndEchoString(IAsyncResult asyncResult)
  38.     {
  39.         Func<string, string> func = ((System.Runtime.Remoting.Messaging.AsyncResult)asyncResult).AsyncDelegate as Func<string, string>;
  40.         return func.EndInvoke(asyncResult);
  41.     }
  42.  
  43.     private string EchoStringDoWork(string input) { return input; }
  44. }

Now for setting up the service. This operation behavior needs an instance of an inspector, so it cannot be defined as an attribute (attribute arguments cannot be user-defined objects). We’ll define two inspectors, one for the calculator (no negative numbers) and one for the asynchronous operations (no null return values):

  1. class NoNegativesInspector : IBetterParameterInspector
  2. {
  3.     public object BeforeCall(string operationName, object[] inputs)
  4.     {
  5.         for (int i = 0; i < inputs.Length; i++)
  6.         {
  7.             inputs[i] = Math.Abs((int)inputs[i]);
  8.         }
  9.  
  10.         return null;
  11.     }
  12.  
  13.     public void AfterCall(string operationName, object[] outputs, ref object returnValue, object correlationState)
  14.     {
  15.         returnValue = Math.Abs((int)returnValue);
  16.         if (outputs != null)
  17.         {
  18.             for (int i = 0; i < outputs.Length; i++)
  19.             {
  20.                 outputs[i] = Math.Abs((int)outputs[i]);
  21.             }
  22.         }
  23.     }
  24. }
  25.  
  26. class NoNullReturnValue : IBetterParameterInspector
  27. {
  28.     public object BeforeCall(string operationName, object[] inputs)
  29.     {
  30.         return null;
  31.     }
  32.  
  33.     public void AfterCall(string operationName, object[] outputs, ref object returnValue, object correlationState)
  34.     {
  35.         if (returnValue == null)
  36.         {
  37.             returnValue = "<<null>>";
  38.         }
  39.     }
  40. }

Now for setting up the service itself (and calling it). In one of the endpoints the behavior is applied to all operations; in the other, the behavior is applied to one operation explicitly.

  1. static void Main(string[] args)
  2. {
  3.     string baseAddress = "https://" + Environment.MachineName + ":8000/Service";
  4.     ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
  5.     ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof(ITest), new BasicHttpBinding(), "");
  6.     ServiceEndpoint asyncEndpoint = host.AddServiceEndpoint(typeof(ITestWithAsyncOperation), new BasicHttpBinding(), "async");
  7.  
  8.     foreach (OperationDescription od in endpoint.Contract.Operations)
  9.     {
  10.         od.Behaviors.Add(new BetterParameterInspectorOperationBehavior(new NoNegativesInspector()));
  11.     }
  12.  
  13.     asyncEndpoint.Contract.Operations.Find("EchoString").Behaviors.Add(
  14.         new BetterParameterInspectorOperationBehavior(new NoNullReturnValue()));
  15.     host.Open();
  16.  
  17.     ChannelFactory<ITest> factory = new ChannelFactory<ITest>(new BasicHttpBinding(), new EndpointAddress(baseAddress));
  18.     ITest proxy = factory.CreateChannel();
  19.     Console.WriteLine("Abs(Abs(33) + Abs(-44)) = {0}", proxy.Add(33, -44));
  20.     Console.WriteLine("Abs(Abs(33) - Abs(-44)) = {0}", proxy.Subtract(33, -44));
  21.     Console.WriteLine("Abs(Abs(33) * Abs(-44)) = {0}", proxy.Multiply(33, -44));
  22.     ((IClientChannel)proxy).Close();
  23.     factory.Close();
  24.  
  25.     ChannelFactory<ITestWithAsyncOperation> asyncFactory = new ChannelFactory<ITestWithAsyncOperation>(new BasicHttpBinding(), new EndpointAddress(baseAddress + "/async"));
  26.     ITestWithAsyncOperation asyncProxy = asyncFactory.CreateChannel();
  27.     Console.WriteLine(asyncProxy.EndEchoString(asyncProxy.BeginEchoString("hello", null, null)));
  28.     Console.WriteLine(asyncProxy.EndEchoString(asyncProxy.BeginEchoString(null, null, null)));
  29.     ((IClientChannel)asyncProxy).Close();
  30.     asyncFactory.Close();
  31. }

That is it. This is an example on how an operation behavior can be used to implement this scenario. Notice that it only works for the server side, as there’s no invoker at the client side (for that side I think I’d use an IClientMessageFormatter). If there is enough demand I’ll expand that scenario in the upcoming post for that interface.

Coming up

We’ll look at the interfaces for the runtime, starting at the inspection interfaces (IClientMessageInspector / IDispatchMessageInspector / IParameterInspector).

[Code for this post]

[Back to the index]

Carlos Figueira
https://blogs.msdn.com/carlosfigueira
Twitter: @carlos_figueira https://twitter.com/carlos_figueira