.net core JilOutputFormatter:不允许同步操作。调用WriteAsync或将AllowSynchronousIO改为true

.net core JilOutputFormatter:不允许同步操作。调用WriteAsync或将AllowSynchronousIO改为true,.net-core,c#-8.0,jil,asp.net-core-3.1,.net-core-3.1,.net Core,C# 8.0,Jil,Asp.net Core 3.1,.net Core 3.1,我在我的.NET Core 3.1项目中有这个格式化程序(我最近从2.1升级了它): 我用以下代码片段将其添加到管道中: services.AddMvcCore(o => { o.OutputFormatters.Insert(0, new JilOutputFormatter()); }).AddOthersBlahBlah(); 当应用程序在2.1上运行时,它就像一个魔咒。但是现在在3.1上,我得到了这个错误: 处理请求时发生未处理的异常。 InvalidOperationE

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

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

services.AddMvcCore(o => {
    o.OutputFormatters.Insert(0, new JilOutputFormatter());
}).AddOthersBlahBlah();
当应用程序在
2.1
上运行时,它就像一个魔咒。但是现在在
3.1
上,我得到了这个错误:

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

我尝试异步写入操作,但在
Jil
上找不到该方法。你有什么想法吗


注意:我知道有一些答案,比如说如何
允许同步IO
。但是我感兴趣的是如何异步编写
Jil

您必须使用3.0 alpha版本。Jil在源代码中的最新稳定版本2.17(或Github搜索有一些问题)

版本3.0直接使用管道。您可以使用。也许您可以使用HttpContext.Response.BodyWriter。我还没有测试过这个

例如:


错误可能围绕
ReadAsync
WriteAsync
FlushAsync
旋转,输出类似于下面列出的内容

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.
作为临时解决方法,您可以在
启动
类中找到的
配置服务
方法中设置
允许同步IO
的值

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

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

    // other services
}
public void配置服务(IServiceCollection服务)
{
配置(选项=>
{
options.AllowSynchronousIO=true;
});
//如果使用IIS:
配置(选项=>
{
options.AllowSynchronousIO=true;
});
//其他服务
}
这不是一个很好的解决办法,但它会让你继续前进更好的解决方案是升级库并异步执行所有操作。


查看Khalid Abuhakmeh的详细帖子

看起来你不能。实际写入发生在
JSON.Serialize
内部。如果Jil本身不使用异步操作,您就什么都做不了。如果在Jil repo.BTW中搜索,您甚至找不到单词
Task
。Jil和其他序列化程序(据推测)速度更快的原因是它们比JSON.NET限制得多。当当前版本为12时,Jil的基准是针对JSON 6的。您应该自己使用BenchmarkDotNet运行一些基准测试,以查看这两个序列化程序和System.Text.JSON之间是否存在有意义的性能和分配差异。如果是,您必须决定是否有理由使用同步IO。在吉尔的回购协议中。不出现在代码中,出现在4个不相关的任务中
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.
public void ConfigureServices(IServiceCollection services)
{
    services.Configure<KestrelServerOptions>(options =>
    {
        options.AllowSynchronousIO = true;
    });

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

    // other services
}