Using ConfigureAwait in "await using" declaration #2661
-
Version Used: Steps to Reproduce: When running the code below, the compiler says namespace test
{
class AwaitUsingConfigureAwaitTest: IAsyncDisposable
{
public async ValueTask DisposeAsync()
{
await Task.CompletedTask;
}
public async Task FooBar()
{
await using (var test = new AwaitUsingConfigureAwaitTest().ConfigureAwait(false))
{
test.Dummy();
}
}
public void Dummy() { }
}
} That means the code has to be written like this. public async Task FooBar()
{
var test = new AwaitUsingConfigureAwaitTest();
await using (test.ConfigureAwait(false))
{
test.Dummy();
}
} Not as succinct as before. Is this the final state of this feature or is there some work still pending around this area? |
Beta Was this translation helpful? Give feedback.
Replies: 11 comments 4 replies
-
Tagging @stephentoub as well |
Beta Was this translation helpful? Give feedback.
-
This is the final state of the feature for C# 8. |
Beta Was this translation helpful? Give feedback.
-
var connection = new SqlConnection(Options.ConnectionString);
await using (connection.ConfigureAwait(false))
{
var command = new SqlCommand(Query, connection) { CommandTimeout = Options.CommandTimeout };
await using (command.ConfigureAwait(false))
{
await connection.OpenAsync().ConfigureAwait(false);
var sqlDependency = new SqlDependency(command);
sqlDependency.OnChange += SqlDependencyOnChange;
CurrentSqlDependency = sqlDependency;
await using var reader = (await command.ExecuteReaderAsync().ConfigureAwait(false)).ConfigureAwait(false);
}
} Instead of this: await using var sc = new SqlConnection(Options.ConnectionString);
await using var command = new SqlCommand(Query, sc) { CommandTimeout = Options.CommandTimeout };
await sc.OpenAsync().ConfigureAwait(false);
var sqlDependency = new SqlDependency(command);
sqlDependency.OnChange += SqlDependencyOnChange;
CurrentSqlDependency = sqlDependency;
await using var reader = await command.ExecuteReaderAsync().ConfigureAwait(false); I think it needs to be reviewed. I almost don't know situations when we need to continue on captured context. Even in UI I just use something like Dispatcher.Invoke or SynchronizationContext.Post and don't worry about context. |
Beta Was this translation helpful? Give feedback.
-
The As some examples from discussions here before: async Task Foo() {
await AsyncHelper.LeaveSynchronizationContext();
... // effectively all awaits here behave like ConfigureAwait(false)'ed
} Or async Task Foo() {
using (AsyncHelper.NoSynchronizationContext()) {
... // effectively all awaits here behave like ConfigureAwait(false)'ed
}
} Or with some compiler magic to make it nicer: [ConfigureAwait(false)] // or maybe name it [NoSynchronizationContext]
async Task Foo() {
... // effectively all awaits here behave like ConfigureAwait(false)'ed
} This can also relief the complains on |
Beta Was this translation helpful? Give feedback.
-
You can also workaround this by abusing out parameter variable declarations: public static class AsyncDisposableHelpers
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IAsyncDisposable AsAsyncDisposable<T>(this T disposable, out T disposableOut) where T : notnull, IAsyncDisposable =>
disposableOut = disposable;
} then you can write await using (new AwaitUsingConfigureAwaitTest().AsAsyncDisposable(out var test).ConfigureAwait(false))
{
test.Dummy();
} |
Beta Was this translation helpful? Give feedback.
-
Years after, I think the question is still valid and unanswered. Generaly no matter what you do, some of the Code Analysis warning, if they're enabled, are triggered. This problem should be officially solved by Microsoft. |
Beta Was this translation helpful? Give feedback.
-
This hasn't been mentioned here; using Fody.ConfigureAwait is an easy solution to that mess. It would be great to have a built-in solution though. |
Beta Was this translation helpful? Give feedback.
-
ran into this issue today. Had to use 2 lines 😢. var efContext = await _efContextFactory.CreateDbContextAsync(cancellationToken).ConfigureAwait(false);
await using var __ = efContext.ConfigureAwait(false); Cannot use an |
Beta Was this translation helpful? Give feedback.
-
Just came here to state my annoyance. Using |
Beta Was this translation helpful? Give feedback.
-
#8778 is a language proposal which describes the various issues surrounding this problem. Please consider upvoting that as a way of representing your annoyance and/or make suggestions on it (because this is a 'question' which has been answered). |
Beta Was this translation helpful? Give feedback.
This is the final state of the feature for C# 8.