I have started using ConfigureAwait(false)
with all the async sql objects.
connection.OpenAsync().ConfigureAwait(false);
cmd.ExecuteNonQueryAsync().ConfigureAwait(false);
But my concern is, Will there be any implications with this approach?.
As this will run on a thread pool which is a separate thread from which it got originated i am not sure about the consequences if we dont run it on a single thread.
Our application is wcf service which will process the 1000's of records parallel.
If some one helps to identity the business scenarios where it could be problem that would be helpful.
Thanks
Configure await This is what the ConfigureAwait method enables to do. Calling ConfigureAwait(false) after the task means that we do not care if the code after the await, runs on the captured context or not. In the output console, “True” will be printed since the synchronization context is not kept.
If the await task. ConfigureAwait(false) involves a task that's already completed by the time it's awaited (which is actually incredibly common), then the ConfigureAwait(false) will be meaningless, as the thread continues to execute code in the method after this and still in the same context that was there previously.
If we set 'ConfigureAwait(true)' then the continuation task runs on the same thread used before the 'await' statement. If we set 'ConfigureAwait(false)' then the continuation task runs on the available thread pool thread.
A situation to use ConfigureAwait(true) is when performing await in a lock, or using any other context/thread specific resources. This requires a synchronization context, which you will have to create, unless you are using Windows Forms or WPF, which automatically create a UI synchronization context.
As a general rule, as long as a region of async operations are self contained and independent, you should be fine using ConfigureAwait(false)
- and indeed doing so can be important to reduce overheads and bottlenecks. The library code usually doesn't need to know about the call-context. However, consuming code (such as winforms, MVC, etc) usually needs to get back to the appropriate context, so should not use ConfigureAwait(false)
. For example:
async Task SomeUXCodeAsync() {
var data = await GetSomeDataAsync(); // note no ConfigureAwait(false)
// not shown: use "data"
}
async Task<Foo> GetSomeDataAsync() {
using(var conn = CreateConnection()) {
await conn.OpenAsync().ConfigureAwait(false);
...
int result = await cmd.ExecuteNonQueryAsync().ConfigureAwait(false);
...
return ...
}
}
The above scenario is pretty typical and common, but it is more complex than that - the TransactionScope
example from the comments touches on examples where the data related code might need to know about the call context, for example. But nuance aside: as long as the consuming code remembers not to ignore the call context, you'll usually end up back at the right place. Sorry this is a little vague and woolly, but : sadly that's kinda the case generally for the call-context.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With