Skip to content

6. How to enable WCF profiling?

Teddy edited this page Nov 17, 2015 · 1 revision

To enable WCF profiling, you need to add the WcfProfilingBehavior to your WCF endpoint programatically or via endpoint configuration. Below is an exampl for both WCF service endpoint and client endpoint to use WcfProfilingBehavior:

<system.serviceModel>
<bindings>
  <basicHttpBinding>
	<binding name="BasicHttpBinding_IWcfDemoService" />
  </basicHttpBinding>
</bindings>
<client>
  <endpoint address="http://localhost:64511/WcfDemoService.svc"
	binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IWcfDemoService"
	contract="DemoService.IWcfDemoService" name="BasicHttpBinding_IWcfDemoService" />
</client>
<behaviors>
  <serviceBehaviors>
	<behavior name="">
	  <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
	  <serviceDebug includeExceptionDetailInFaults="false" />
	</behavior>
  </serviceBehaviors>
  <endpointBehaviors>
	<behavior>
	  <tinyprofiler />
	</behavior>
  </endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
  multipleSiteBindingsEnabled="true" />
<extensions>
  <behaviorExtensions>
	<add name="tinyprofiler" type="EF.Diagnostics.Profiling.ServiceModel.Configuration.WcfProfilingBehaviorElement, NanoProfiler.Wcf"/>
  </behaviorExtensions>
</extensions>
</system.serviceModel>

What happens when WCF profiling is enabled is:

  • For client endpoint, each WCF service call in your code will be logged as a WCF timing as one of the custom timings, similar to DB timing, in the profiling result, both the WCF action name and the WCF request message XML is logged as properties of a WCF timing;
  • For service endpoint, each WCF service method call is logged as a separate profiling session;

Please check the source code of NanoProfiler.Demos.SimpleDemo in source code of NanoProfiler on github if you want a running example.

NOTICE:

When you have both WCF services (using HTTP bindings) and some other web/rest services deployed in the same web application, if you enables profiling on both WCF services and web/rest services globally in application_start event, if you don't have set in web.config, you will get two profiling sessions be logged.

The reason is, when NOT aspNetCompatibilityEnabled="true", for a WCF service request, the WCF method execution and application_beginrequest/application_endrequest event handler execution are in different threads. So when the profiling session is being started, it could not access the profiling session which has already been started in application_beginrequest.