question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Improve async handling of OperationContextScope

See original GitHub issue

If 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:closed
  • Created 6 years ago
  • Reactions:10
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
zhenlancommented, Jan 26, 2018

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.

1reaction
mconnewcommented, Oct 16, 2020

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

OperationContextScope Class (System.ServiceModel)
When an OperationContextScope is created, the current OperationContext is stored and the new OperationContext becomes the one returned by the Current property.
Read more >
This OperationContextScope is being disposed out of order
This OperationContextScope is being disposed out of order. I believe I am using wrong pattern to call WCF service using async/await but I...
Read more >
WCF OperationContext.Current problem for async operation
This issue occurs when one creates an OperationContextScope and proceeds to call OperationContext.Current within the using clause. A common ...
Read more >
[Solved]-Async WCF client calls with custom headers
Coding example for the question Async WCF client calls with custom headers: This OperationContextScope is being disposed out of order-C#.
Read more >
WCF async / await - propagating OperationContext - tin oscillator
After the first await statement in your async service operation, OperationContext.Current could be null because the rest of the method body may ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found