JilOutputFormatter:不允许进行同步操作。调用WriteAsync或将AllowSynchronousIO设置为true

我的.NET Core 3.1项目中有这个格式化程序(我最近从2.1升级了):

public class JilOutputFormatter : TextOutputFormatter {


    public JilOutputFormatter() => 
        JilFormatterConfig.AddSupportedHeaders(SupportedMediaTypes,SupportedEncodings);

    public override Task WriteResponseBodyAsync(OutputFormatterWriteContext context,Encoding selectedEncoding) {
        using (var writer = new StreamWriter(context.HttpContext.Response.Body)) {
            JSON.Serialize(context.Object,writer,myoptions);
            writer.Flush();
        }

        return Task.FromResult(true);
    }

}

我使用以下代码片段将其添加到管道中:

services.AddMvcCore(o => {
    o.OutputFormatters.Insert(0,new JilOutputFormatter());
}).AddOthersBlahBlah();

2.1上运行该应用程序时,它就像一种魅力。但是现在在3.1上我遇到了这个错误:

  

处理请求时发生未处理的异常。   InvalidOperationException:不允许进行同步操作。呼叫   将WriteAsync或将AllowSynchronousIO设置为true。

我试图异步执行写操作,但是在Jil上找不到该方法。你有什么主意吗?

注意::我知道有一些答案,例如this one,它们说明了如何AllowSynchronousIO。但是我对如何异步写入Jil感兴趣。

wosongxiangyi 回答:JilOutputFormatter:不允许进行同步操作。调用WriteAsync或将AllowSynchronousIO设置为true

您将必须使用3.0 alpha版本。最新的稳定版本2.17中的源代码中的Jil doesn't even include the word Task(或Github搜索出现问题)。

3.0版直接使用管道。您可以使用SerializeAsync(T,PipeWriter,Encoding,Options,CancellationToken)也许您可以使用HttpContext.Response.BodyWriter。我还没有测试过。

例如:

public override async Task WriteResponseBodyAsync(OutputFormatterWriteContext context,Encoding selectedEncoding) 
{
    var data=context.Object;
    var writer=contest.Response.BodyWriter;
    await JSON.SerializeAsync(data,writer,selectedEncoding);
}
,

错误可能围绕ReadAsyncWriteAsyncFlushAsync发生,其输出类似于下面列出的内容。

Synchronous operations are disallowed. Call ReadAsync or set AllowSynchronousIO to true instead.

Synchronous operations are disallowed. Call WriteAsync or set AllowSynchronousIO to true instead.

Synchronous operations are disallowed. Call FlushAsync or set AllowSynchronousIO to true instead.

作为临时的解决方法,您可以在AllowSynchronousIO类中找到的ConfigureServices方法中设置Startup的值。

public void ConfigureServices(IServiceCollection services)
{
    services.Configure<KestrelServerOptions>(options =>
    {
        options.AllowSynchronousIO = true;
    });

    // If using IIS:
    services.Configure<IISServerOptions>(options =>
    {
        options.AllowSynchronousIO = true;
    });

    // other services
}

这不是一个很好的解决方法,但可以让您继续前进。 更好的解决方案是升级库并异步执行所有操作。

请参见Khalid Abuhakmeh的详细帖子.NET Core 3.0 AllowSynchronousIO Workaround

本文链接:https://www.f2er.com/2919067.html

大家都在问