'.NET Core 3 preview: Synchronous operations are disallowed

I have an Angular.js app that I am porting to .NET Core.

It was working fine in the previous version of .NET Core 3 preview; 3.2.

However, after upgrading to latest 3.3 some of the get requests are returning this error:

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

I can't see why this is happening with only some requests and not others.

I believe that by default Angular.js does async: xhr.open(method, url, true);

Can anyone shed some light on this?



Solution 1:[1]

This problem is described here: https://github.com/aspnet/AspNetCore/issues/8302

The workaround for now is to manually set AllowSynchronous to true in startup.cs;

// Startup.ConfigureServices

services.Configure<IISServerOptions>(options =>
{
  options.AllowSynchronousIO = true;
});

Solution 2:[2]

It's worth noting that if you host on kestrel directly then your Program.cs should have appropriate ConfigureKestrel call

   public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .ConfigureKestrel((context, options) =>
                {
                    options.AllowSynchronousIO = true;
                })

Solution 3:[3]

you can disable it for a special method

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

or disable in all application scope

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .ConfigureKestrel((context, options) =>
            {
                options.AllowSynchronousIO = true;
            })

or in service configure startup

services.Configure<IISServerOptions>(options =>
{
  options.AllowSynchronousIO = true;
});

Solution 4:[4]

If you are using a CustomWebApplicationFactory like me, you can set the flag in its constructor, It makes my test direct from VS2019 works.

public class CustomWebApplicationFactory<TStartup> : WebApplicationFactory<TStartup> where TStartup: class
{
    public CustomWebApplicationFactory()
    {
        Server.AllowSynchronousIO = true;
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 TylerH
Solution 2 pkmiec
Solution 3
Solution 4 Bin Chen