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

时间:2020-04-02 04:28:43

标签: c# asp.net-core asynchronous core asp.net-core-3.1

我有一个针对AspNetCore 2.2的REST API,该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);                
    }

这是下载方法,它创建了我要在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);
            }
        }
    }

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

2 个答案:

答案 0 :(得分:2)

默认情况下(AllowSynchronousIO.Net core 3.0.0-preview3KestrelHttpSys中的IIS in-process中的

TestServer选项被禁用,因为这些API是源线程饥饿和应用程序挂起。

对于每个临时迁移请求,该选项均可被覆盖:

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

您可以找到更多信息并关注ASP.NET Core问题跟踪器:AllowSynchronousIO disabled in all servers

答案 1 :(得分:2)

通过在退出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();
}

通过这种方式,您可以在不做AllowSynchronousIO = true

的情况下对代码进行少量更改