如何使 "await using" 语法正确?
How do I get the "await using" syntax correct?
我有以下同步代码,工作正常:
private void GenerateExportOutput()
{
using StreamWriter writer = new(Coordinator.OutputDirectory + @"\export.txt");
if (this.WikiPagesToExport.IsEmpty)
{
return;
}
var wanted = new SortedDictionary<string, WikiPage>(this.WikiPagesToExport, StringComparer.Ordinal);
foreach (var title in wanted.Keys)
{
writer.WriteLine(title);
}
}
我想改成异步的。所以:
private async Task GenerateExportOutputAsync()
{
using StreamWriter writer = new(Coordinator.OutputDirectory + @"\export.txt");
if (this.WikiPagesToExport.IsEmpty)
{
return;
}
var wanted = new SortedDictionary<string, WikiPage>(this.WikiPagesToExport, StringComparer.Ordinal);
foreach (var title in wanted.Keys)
{
await writer.WriteLineAsync(title).ConfigureAwait(false);
}
await writer.FlushAsync().ConfigureAwait(false);
}
编译。但是我使用的其中一个分析器 (Meziantou.Analyzer) 现在建议我“更喜欢使用 'await using'”。我从来没有使用过 await using(尽管我过去曾尝试过几次,并且总是 运行 遇到我现在 运行 遇到的相同问题)。但是我想用它,所以:
await using StreamWriter writer = new StreamWriter(OutputDirectory + @"\export.txt").ConfigureAwait(false);
现在它不再编译:CS0029 Cannot implicitly convert type 'System.Runtime.CompilerServices.ConfiguredAsyncDisposable' to 'System.IO.StreamWriter'
。好的,好的,所以我将其改为使用 var
:
await using var writer = new StreamWriter(OutputDirectory + @"\export.txt").ConfigureAwait(false);
它通过了 CS0029,但现在后面的代码无法编译:Error CS1061 'ConfiguredAsyncDisposable' does not contain a definition for 'WriteLineAsync'
(和 FlushAsync
的类似代码)。太棒了......也许可以转换它?
await ((StreamWriter)writer).WriteLineAsync(title).ConfigureAwait(false);
没有:Error CS0030 Cannot convert type 'System.Runtime.CompilerServices.ConfiguredAsyncDisposable' to 'System.IO.StreamWriter'
我在谷歌上搜索和阅读了很多内容,包括现在和过去的几次,但我完全无法弄清楚如何使用这个“等待使用”的东西。我该怎么做?谢谢。
当前的 await using
语法 (C# 10) 在支持配置等待 IAsyncDisposable
方面还有很多不足之处。我们能做的最好的是:
private async Task GenerateExportOutputAsync()
{
StreamWriter writer = new(Coordinator.OutputDirectory + @"\export.txt");
await using (writer.ConfigureAwait(false))
{
//...
}
}
...这并不比根本不使用 await using
语法更紧凑:
private async Task GenerateExportOutputAsync()
{
StreamWriter writer = new(Coordinator.OutputDirectory + @"\export.txt");
try
{
//...
}
finally { await writer.DisposeAsync().ConfigureAwait(false); }
}
相关 GitHub 问题:Using ConfigureAwait in "await using" declaration。
我有以下同步代码,工作正常:
private void GenerateExportOutput()
{
using StreamWriter writer = new(Coordinator.OutputDirectory + @"\export.txt");
if (this.WikiPagesToExport.IsEmpty)
{
return;
}
var wanted = new SortedDictionary<string, WikiPage>(this.WikiPagesToExport, StringComparer.Ordinal);
foreach (var title in wanted.Keys)
{
writer.WriteLine(title);
}
}
我想改成异步的。所以:
private async Task GenerateExportOutputAsync()
{
using StreamWriter writer = new(Coordinator.OutputDirectory + @"\export.txt");
if (this.WikiPagesToExport.IsEmpty)
{
return;
}
var wanted = new SortedDictionary<string, WikiPage>(this.WikiPagesToExport, StringComparer.Ordinal);
foreach (var title in wanted.Keys)
{
await writer.WriteLineAsync(title).ConfigureAwait(false);
}
await writer.FlushAsync().ConfigureAwait(false);
}
编译。但是我使用的其中一个分析器 (Meziantou.Analyzer) 现在建议我“更喜欢使用 'await using'”。我从来没有使用过 await using(尽管我过去曾尝试过几次,并且总是 运行 遇到我现在 运行 遇到的相同问题)。但是我想用它,所以:
await using StreamWriter writer = new StreamWriter(OutputDirectory + @"\export.txt").ConfigureAwait(false);
现在它不再编译:CS0029 Cannot implicitly convert type 'System.Runtime.CompilerServices.ConfiguredAsyncDisposable' to 'System.IO.StreamWriter'
。好的,好的,所以我将其改为使用 var
:
await using var writer = new StreamWriter(OutputDirectory + @"\export.txt").ConfigureAwait(false);
它通过了 CS0029,但现在后面的代码无法编译:Error CS1061 'ConfiguredAsyncDisposable' does not contain a definition for 'WriteLineAsync'
(和 FlushAsync
的类似代码)。太棒了......也许可以转换它?
await ((StreamWriter)writer).WriteLineAsync(title).ConfigureAwait(false);
没有:Error CS0030 Cannot convert type 'System.Runtime.CompilerServices.ConfiguredAsyncDisposable' to 'System.IO.StreamWriter'
我在谷歌上搜索和阅读了很多内容,包括现在和过去的几次,但我完全无法弄清楚如何使用这个“等待使用”的东西。我该怎么做?谢谢。
当前的 await using
语法 (C# 10) 在支持配置等待 IAsyncDisposable
方面还有很多不足之处。我们能做的最好的是:
private async Task GenerateExportOutputAsync()
{
StreamWriter writer = new(Coordinator.OutputDirectory + @"\export.txt");
await using (writer.ConfigureAwait(false))
{
//...
}
}
...这并不比根本不使用 await using
语法更紧凑:
private async Task GenerateExportOutputAsync()
{
StreamWriter writer = new(Coordinator.OutputDirectory + @"\export.txt");
try
{
//...
}
finally { await writer.DisposeAsync().ConfigureAwait(false); }
}
相关 GitHub 问题:Using ConfigureAwait in "await using" declaration。