缓冲的流-ASP.NET Core 3.0中不允许进行同步操作 [英] Buffered Stream - Synchronous operations are disallowed in ASP.NET Core 3.0
问题描述
我有一个针对AspNetCore 2.2的REST API,该API的端点允许下载一些大的json文件.迁移到AspNetCore 3.1后,此代码停止工作:
试试{HttpContext.Response.StatusCode =(int)HttpStatusCode.OK;HttpContext.Response.Headers.Add("Content-Type","application/json");使用(var bufferedOutput = new BufferedStream(HttpContext.Response.Body,bufferSize:4 * 1024 * 1024)){等待_downloadService.Download(_applicationId,bufferedOutput);}}抓住(前例外){_logger.LogError(ex,ex.Message);}
这是下载方法,它创建了我要在HttpContext.Response.Body上返回的json:
公共异步任务下载(字符串applicationId,流输出,CancellationToken cancelToken =默认(CancellationToken)){使用(var textWriter = new StreamWriter(output,Constants.Utf8)){使用(var jsonWriter = new JsonTextWriter(textWriter)){jsonWriter.Formatting = Formatting.None;等待jsonWriter.WriteStartArrayAsync(cancellationToken);//写入json ...等待jsonWriter.WritePropertyNameAsync("Status",cancelledToken);等待jsonWriter.WriteValueAsync(someStatus,cancelledToken);等待jsonWriter.WriteEndArrayAsync(cancellationToken);}}}
现在,我收到此异常:"ASP.NET Core 3.0中不允许进行同步操作"我如何在不使用AllowSynchronousIO = true的情况下更改此代码以使其工作?
AllowSynchronousIO
选项默认情况下在( Kestrel
, HttpSys
, IIS进程内
, TestServer
),因为这些API是线程饥饿和应用程序挂起的源./p>
对于每次临时迁移的请求,都可以覆盖该选项:
var allowSynchronousIoOption = HttpContext.Features.Get< IHttpBodyControlFeature>();如果(allowSynchronousIoOption!= null){allowSynchronousIoOption.AllowSynchronousIO = true;}
您可以找到更多信息,并遵循ASP.NET Core问题跟踪器:在所有情况下均禁用AllowSynchronousIO服务器
I had an REST API targeting AspNetCore 2.2 with an endpoint that allows download of some big json files. After migrating to AspNetCore 3.1 this code stopped working:
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);
}
This is the download method, that created the json that I want to return on the HttpContext.Response.Body:
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);
}
}
}
Now I get this exception: "Synchronous operations are disallowed in ASP.NET Core 3.0" How can I change this code to work without using AllowSynchronousIO = true;
AllowSynchronousIO
option is disabled by default from .Net core 3.0.0-preview3
in (Kestrel
, HttpSys
, IIS in-process
, TestServer
) because these APIs are source of thread starvation and application hangs.
The option can be overridden on a per request for a temporary migration :
var allowSynchronousIoOption = HttpContext.Features.Get<IHttpBodyControlFeature>();
if (allowSynchronousIoOption != null)
{
allowSynchronousIoOption.AllowSynchronousIO = true;
}
You can find more info and follow the ASP.NET Core Issue Tracker : AllowSynchronousIO disabled in all servers
这篇关于缓冲的流-ASP.NET Core 3.0中不允许进行同步操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!