Here's a parameter inspector I once wrote to measure performance of my WCF service methods. Notice that a Stopwatch is started and returned in the BeforeCall
method which allows you to retrieve it in the AfterCall
method as the correlationState
parameter:
public class PerformanceCountersInspector : IParameterInspector
{
public object BeforeCall(string operationName, object[] inputs)
{
return Stopwatch.StartNew();
}
public void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState)
{
var watch = (Stopwatch)correlationState;
watch.Stop();
var time = watch.ElapsedMilliseconds;
// Do something with the result
}
}
The difference here is that using a parameter inspector will not take into account the time taken to serialize input/output parameters. It will only account for the operation time. If you want to include serialization time you might use IDispatchMessageInspector. The BeforeSendReply method also has a correlationState
that works the same.
UPDATE:
You could configure the parameter inspector in web.config by writing a behavior extension:
public class PerformanceCountersBehaviorExtension : BehaviorExtensionElement, IServiceBehavior
{
public override Type BehaviorType
{
get { return typeof(PerformanceCountersBehaviorExtension); }
}
protected override object CreateBehavior()
{
return this;
}
void IServiceBehavior.AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
{
}
void IServiceBehavior.ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
foreach (ChannelDispatcher channelDispatcher in serviceHostBase.ChannelDispatchers)
{
foreach (var endpoint in channelDispatcher.Endpoints)
{
foreach (var operation in endpoint.DispatchRuntime.Operations)
{
var inspector = new PerformanceCountersInspector();
operation.ParameterInspectors.Add(inspector);
}
}
}
}
void IServiceBehavior.Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
}
}
And in your configuration file you register the extension:
<services>
<service name="MyAssembly.MyService" behaviorConfiguration="returnFaults">
<endpoint address="" binding="basicHttpBinding" contract="MyAssembly.IMyServiceContract"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="returnFaults">
<serviceDebug includeExceptionDetailInFaults="true"/>
<serviceMetadata httpGetEnabled="true"/>
<perfCounters />
</behavior>
</serviceBehaviors>
</behaviors>
<extensions>
<behaviorExtensions>
<add name="perfCounters" type="MyAssembly.PerformanceCountersBehaviorExtension, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</behaviorExtensions>
</extensions>