microsoft / ApplicationInsights-SDK-Labs

Application Insights experimental projects repository
MIT License
61 stars 48 forks source link

Add checks for Wcf client operation context #47

Closed tomasr closed 8 years ago

tomasr commented 8 years ago

Make the WcfOperationContext more robust with explicit checks to prevent creating a context on a WCF client-side OperationContext instance.

Also add a workaround for issue https://github.com/Microsoft/ApplicationInsights-SDK-Labs/issues/45.

The fundamental issue here is that the WCF design around OperationContext is broken: You can stack contexts through the use of OperationContext.Current, but there is no way to navigate from the child context back to the parent.

The workaround uses CallContext to track the original OperationContext if the service code fails to dispose the OperationContextScope and we end up with OperationContext.Current returning the wrong value.

Not sure if using ThreadLocal<T> coupled with an AsyncLocal<T> here would be better than using CallContext in this case. Reluctant to have to mirror the jumps that WCF OperationContext.Current does.