Improve async handling of OperationContextScope
See original GitHub issueIf you have the following code an exception will be thrown if the continuation happens on a different thread.
using(new OperationContextScope((IContextChannel)channel))
{
await Task.Yield();
}
This is because OperationContextScope.Dispose() makes sure that the scope stored in an internal ThreadLocal matches the instance that is being disposed to prevent trying to dispose an instance that the current executing code doesn’t own. This fails when using async/await. We need to improve this situation by at least not having it throw, and preferably have it flow the context through an async/await. We had some problems with this on the full framework due to interactions with re-entrant services. We shouldn’t have the same problems on .NET Core as we don’t support services, and we don’t support the ConcurrencyMode property on CallbackBehaviorAttribute which would enable setting the concurrency mode to Reentrant.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:10
- Comments:5 (3 by maintainers)

Top Related StackOverflow Question
Triage discussion: we should come up a solution that can work on both .NET Core and .NET Framework. For example, @mconnew proposed the idea of introducing a new AsyncOperationContextScope.
I just discovered the documentation for this is pretty hard to parse as it’s buried deep in a migration document. The easiest place to get info is actually this github issue: https://github.com/Microsoft/dotnet/issues/403