How do I get WCF to propagate the properties of Trace.CorrelationManager when it uses a different thread for the same operation?
I know that WCF doesn’t guarantee thread affinity. So basically one thread can start a request and a different thread may finish it. When I reproed this behavior, I see that the first thread has the two properties Trace.Correlation.ActivityId and Trace.Correlation.LogicalOperationStack properly set. WCF finished the operation with a different thread but the properties were not propagated.
To work around this, I might have to abandon the use of the CorrelationManager and will probably have to store the ActivityId in the OperationContext which I know is propagated to the new thread (Please correct me if I am wrong here). I don’t want to do this, since its more work of course and not as elegant as using that single property.
Any other ideas on how I can work around this? Can I tell WCF to propagte this for me somehow?
Thanks,
Mohammed
To work around this, I abandoned the use of Trace.CorrelationManager properties. As a substitute to these properties, I now use custom properties that I added to an Extension class. I can now directly modify the ActivityId and LogicalOperationStack on this extension which can be accessed for the life of any operation request which is just as convenient as using the Trace.CorrelationManager properties.
As a plus, I can store any other custom properties I would like to use for the life of the request. A great example of this would be a customer id, or resource id, that can also be used in logging for better supportability.
You will now need add this extension to the OperationContext. First, you need to find a suitable hook into a WCF behavior extension. A popular option is to use a MessageInspector applied to an IEndpointBehavior. Here are some great reads on how to achieve this (A quick search will yield many useful examples if mine don’t help):
Once you have your hook, you want to add your Extension to the OperationContext as soon as you can with the following line:
Now you can access your ActivityId property and LogicalOperationStack, or any other property you define in this class from virtually anywhere in your code work flow:
Hope this helps!
-Mohammed