缓冲流 - ASP.NET Core 3.0 中不允许同步操作

Ang*_*ela 4 c# asynchronous asp.net-core asp.net-core-3.1

我有一个针对 AspNetCore 2.2 的 REST API,其端点允许下载一些大的 json 文件。迁移到 AspNetCore 3.1 后,此代码停止工作:

    try
    {
        HttpContext.Response.StatusCode = (int)HttpStatusCode.OK;
        HttpContext.Response.Headers.Add("Content-Type", "application/json");

        using (var bufferedOutput = new BufferedStream(HttpContext.Response.Body, bufferSize: 4 * 1024 * 1024))
        {
            await _downloadService.Download(_applicationId, bufferedOutput);
        }
    }
    catch (Exception ex)
    {
        _logger.LogError(ex, ex.Message);                
    }
Run Code Online (Sandbox Code Playgroud)

这是下载方法,它创建了我想要在 HttpContext.Response.Body 上返回的 json:

    public async Task Download(string applicationId, Stream output, CancellationToken cancellationToken = default(CancellationToken))
    {       
        using (var textWriter = new StreamWriter(output, Constants.Utf8))
        {
            using (var jsonWriter = new JsonTextWriter(textWriter))
            {
                jsonWriter.Formatting = Formatting.None;
                await jsonWriter.WriteStartArrayAsync(cancellationToken);

                //write json...
                await jsonWriter.WritePropertyNameAsync("Status", cancellationToken);
                await jsonWriter.WriteValueAsync(someStatus, cancellationToken); 

                await jsonWriter.WriteEndArrayAsync(cancellationToken);
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

现在我得到这个异常:“ASP.NET Core 3.0 中不允许同步操作”如何更改此代码以在不使用AllowSynchronousIO = true; 的情况下工作?

小智 5

AllowSynchronousIO.Net core 3.0.0-preview3默认情况下, ( KestrelHttpSysIIS in-processTestServer)中的选项被禁用,因为这些 API 是线程匮乏和应用程序挂起的根源。

可以根据临时迁移的每个请求覆盖该选项:

var allowSynchronousIoOption = HttpContext.Features.Get<IHttpBodyControlFeature>();
if (allowSynchronousIoOption != null)
{
    allowSynchronousIoOption.AllowSynchronousIO = true;
}
Run Code Online (Sandbox Code Playgroud)

您可以找到更多信息并关注 ASP.NET Core 问题跟踪器:AllowSynchronousIO 在所有服务器中禁用


Yas*_*eda 5

通过在退出 using 子句之前调用 FlushAsync 和 DisposeAsync,您可以停止写入器处置期间发生的同步操作。BufferedStream似乎有同步写入问题,如何控制StreamWriter中的缓冲区大小。

using (var streamWriter = new StreamWriter(context.Response.Body, Encoding.UTF8, 1024))
using (var jsonWriter = new JsonTextWriter(streamWriter))
{
    jsonWriter.Formatting = Formatting.None;
    await jsonWriter.WriteStartObjectAsync();
    await jsonWriter.WritePropertyNameAsync("test");
    await jsonWriter.WriteValueAsync("value " + new string('a', 1024 * 65));
    await jsonWriter.WriteEndObjectAsync();

    await jsonWriter.FlushAsync();
    await streamWriter.FlushAsync();
    await streamWriter.DisposeAsync();
}
Run Code Online (Sandbox Code Playgroud)

通过这种方式,你可以让你的代码只需稍加改动就可以工作,而无需AllowSynchronousIO = true