7
votes

I have a problem with OperationContext getting null after an async operation is called (and my threadid changes).

I know this is a know issue and I've gone threw some StackOverflow questions regarding the issue.

In .net 4.6.2 there is a fix for the issue as you can read here.

OperationContext.Current Async Improvements

WCF now has the ability to include OperationContext.Current with ExecutionContext so that the OperationContext flows through asynchronous continuations. With this improvement, WCF allows CurrentContext to propagate from one thread to another thread. This means that even if there’s a context switch between calls to OperationContext.Current, it’s value will flow correctly throughout the execution of the method.

Is there anything special I need to do in order to get this supported on my end? I'm using VS 2013, updated the framework to 4.6.2 and installed the dev-pack. I've changed my project to use Framework 4.6.2 and I still get a null OperationContext after an async call.

3
Does this fix WebOperationContext too?Rhyous

3 Answers

5
votes

Per the answer from Tomasz, make sure the following is in your app config:

<appSettings>
    <add key="wcf:disableOperationContextAsyncFlow" value="false" />
</appSettings>

For more information take a look at https://github.com/Microsoft/dotnet/issues/403 where MS acknowledges that this is, in fact, a breaking change. Seems like this could easily break a lot applications out in the field.

1
votes

After installing KB4013429 operation context async flow is disabled by default. To enable it (and use functionality fixed in 4.6.2) you need to set wcf:disableOperationContextAsyncFlow flag to "false" in your app config. (I haven't found any information about this flag, discovered it while fixing issue with incorrect propagation of OperationContext).

0
votes

The behavior that you're describing managed to make it into .NET 4.6.2 release and we're aware of it. In fact, we're in the process of rolling out a fix for this and I'm expecting it to be publicly available within the next couple of months.

For now the known workarounds are to revert to 4.6.1 version of the framework or to do something similar to this:

OperationContext ocx = OperationContext.Current;

using (new OperationContextScope(OperationContext.Current))
    {
        OperationContext.Current = new OperationContext(ocx.Channel);
        // ...
    }

Please let us know if you have further questions or comments.