wcfContext.InstanceContext is null

When wcfContext.InstanceContext is null the miniprofiler crashs

I don’t know how i made wcfContext.InstanceContext be null, but i don’t believe the miniprofiler should crash the host application.

If i don’t use wcf miniprofiler behavior the application works fine.

similar problem here:

in our case it crashes here:

WcfInstanceContext GetCurrentWithoutInstantiating() {
line 74:  return wcfContext.InstanceContext.Extensions.Find<WcfInstanceContext>();

The WCF InstanceContext appears to be null in the following situation:

-we initialize the EF MiniProfiler once at the startup of an appdomain (so outside the context of a WCF call)
-we also register a custom ServiceAuthenticationManager class as part of a custom WCF ServiceBehavior
-this custom ServiceAuthenticationManager class performs data access using EF, which triggers the EF MiniProfiler that was already activated
-the EF MiniProfiler then tries to obtain the current WCF Profiler
-the WCF profiler tries to reference the OperationContext.InstanceContext which is null in this case -> crash

The problem can be solved by changing line 71 to

 if (wcfContext == null || wcfContext.InstanceContext == null)

Btw, it would probably be prudent to apply the same fix to WcfInstanceContext.Current

Can you please try this with the new -beta nugets that have been released and see if it is still an issue?

Hi Yaakov,
before switching to the nuget beta package we inspected the latest source on github. But there seems to be no fix applied to the WcfInstanceContext class yet. Are the nuget beta packages perhaps built from newer code than the source on github? If not it makes little sense for us to try…

I just applied these changes to the code in GitHub.

However, the StackExchange.Profiling.Wcf project current does not have an associated nuget. This is primarily because we don’t use it at all, so it has not been actively maintained. If you are interested in a nuget, then I would be happy to look over any Pull Requests needed to bring the project up to date with version 3, making sure that the WcfSampleController.cs is working in the Sample.Mvc project, and adding a MiniProfiler.Wcf nuspec / nuget.