ASP.NET Core:不允许进行同步操作.调用WriteAsync或将AllowSynchronousIO设置为true [英] ASP.NET Core : Synchronous operations are disallowed. Call WriteAsync or set AllowSynchronousIO to true instead

查看:2876
本文介绍了ASP.NET Core:不允许进行同步操作.调用WriteAsync或将AllowSynchronousIO设置为true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ASP.NET核心服务器,AllowSynchronousIO设置为false

ASP.NET core server, AllowSynchronousIO is set to false

        new WebHostBuilder()
        .UseKestrel(options =>
        {
            options.AllowSynchronousIO = false;
        })

在操作中,它输出一个JsonResult

In the action, it outputs a JsonResult

    public async Task<IActionResult> SanityCheck()
    {
        Dictionary<string, string> dic = await GetDic();

        return this.Json(dic);
    }

它以异常结尾

System.InvalidOperationException:同步操作是 不允许的.调用WriteAsync或将AllowSynchronousIO设置为true.

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

我不能用AllowSynchronousIO=false返回JsonResult吗?

Can't I return a JsonResult with AllowSynchronousIO=false ?

最好的问候

推荐答案

您可能会遇到以下问题: https://github.com/aspnet/AspNetCore/issues/8302

You might have the following problem: https://github.com/aspnet/AspNetCore/issues/8302

您可以在此处找到更多信息: https://github.com/aspnet/AspNetCore/Issues/7644

And you can find more info here: https://github.com/aspnet/AspNetCore/issues/7644

解决此问题之前的解决方法是允许同步IO.将其放入用于Kestrel或IIS的Startup.cs中:

A workaround until the issue is being solved is to allow Synchronous IO. Put this in Startup.cs for either Kestrel or IIS:

public void ConfigureServices(IServiceCollection services)
{
    // If using Kestrel:
    services.Configure<KestrelServerOptions>(options =>
    {
        options.AllowSynchronousIO = true;
    });

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

这篇关于ASP.NET Core:不允许进行同步操作.调用WriteAsync或将AllowSynchronousIO设置为true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆