Time for your operation execution using the Operation invoker.

Have you ever wanted to do something like this . You can just use the default counters for this http://msdn.microsoft.com/en-us/library/ms735098.aspx, but here we try to describe how to build a behavior which we could apply on an operation as shown below.

[code:c#]    // Define a service contract.    [ServiceContract(Namespace="http://Microsoft.ServiceModel.Samples")]    public interface ICalculator    {        [OperationContract]        [PerfOperationBehavior]        double Add(double n1, double n2);        [OperationContract]        double Subtract(double n1, double n2);        [OperationContract]        double Multiply(double n1, double n2);        [OperationContract]        double Divide(double n1, double n2);    }[/code]

Where you probably wanted to write some code to log the time for the operation or just update some performance counter on the server. If you are already familiar with Operation invoker then the rest rest of the post is just a waste of time since i just wanted to show how the operation invoker could be used pretty much inject your code into the pipeline to see what is the execution time of your operation.

These are pretty much the steps you need to do.

  1. Create an operation behavior obviously since you want to attach to the operation.
  2. Implement the IOperationInvoker for kind of intercepting operation invocation.
  3. Attach a counter extension to log the start and end of the operation. 

And you pretty much have your behavior you can just add on the operation.

Here is the behavior and the classes required to implement this and shows a very bare bone implementation of the ticks that would occur between invocations of an operation.

Find the full solution attached below.

[code:c#]using System;using System.Collections.Generic;using System.Text;using System.ServiceModel.Description;using System.ServiceModel.Dispatcher;using System.ServiceModel;using System.Diagnostics;namespace Microsoft.Samples.Wcf.Performance{  [AttributeUsage(AttributeTargets.Method)]  public class PerfOperationBehavior : System.Attribute, IOperationBehavior  {    void IOperationBehavior.AddBindingParameters(OperationDescription operationDescription,      System.ServiceModel.Channels.BindingParameterCollection bindingParameters)    {    }    void IOperationBehavior.ApplyClientBehavior(OperationDescription operationDescription,      System.ServiceModel.Dispatcher.ClientOperation clientOperation)    {    }    void IOperationBehavior.ApplyDispatchBehavior(OperationDescription operationDescription,      System.ServiceModel.Dispatcher.DispatchOperation dispatchOperation)    {      dispatchOperation.Invoker = new PerfCounterOperationInvoker(dispatchOperation.Invoker);    }    void IOperationBehavior.Validate(OperationDescription operationDescription)    {    } } internal class PerfCounterOperationInvoker : IOperationInvoker {    IOperationInvoker innerOperationInvoker;    public PerfCounterOperationInvoker(IOperationInvoker invoker)    {      this.innerOperationInvoker = invoker;    }    object[] IOperationInvoker.AllocateInputs()    {      return this.innerOperationInvoker.AllocateInputs();    }    object IOperationInvoker.Invoke(object instance, object[] inputs, out object[] outputs)    {      OperationContext.Current.Extensions.Add(new PerfOperationContextExtension());      try      {        return this.innerOperationInvoker.Invoke(instance, inputs, out outputs);      }      finally      {        OperationContext.Current.Extensions.Remove(          OperationContext.Current.Extensions.Find());      }    }    IAsyncResult IOperationInvoker.InvokeBegin(object instance, object[] inputs, AsyncCallback callback, object state)    {      OperationContext.Current.Extensions.Add(new PerfOperationContextExtension());      return this.innerOperationInvoker.InvokeBegin(instance, inputs, callback, state);    }    object IOperationInvoker.InvokeEnd(object instance, out object[] outputs, IAsyncResult result)    {      OperationContext.Current.Extensions.Remove(        OperationContext.Current.Extensions.Find());      return this.innerOperationInvoker.InvokeEnd(instance, out outputs, result);    }    bool IOperationInvoker.IsSynchronous    {      get { return this.innerOperationInvoker.IsSynchronous; }    }  }  internal class PerfOperationContextExtension : IExtension  {    long ticks;    public void Attach(OperationContext owner)    {      ticks = DateTime.Now.Ticks;    }    public void Detach(OperationContext owner)    {      ticks = DateTime.Now.Ticks - ticks;      Debug.WriteLine(string.Format("Operation : {0} took {1} ticks",                  owner.IncomingMessageHeaders.Action,                  ticks));    }  }}[/code]