AllowSynchronousIO: Fix 'Synchronous Operations Are Disallowed'
Fix the ASP.NET Core 'Synchronous operations are disallowed' error using AllowSynchronousIO on Kestrel, IIS, and HttpSys, then move to async I/O.
To fix the ASP.NET Core error System.InvalidOperationException: Synchronous operations are disallowed. Call ReadAsync or set AllowSynchronousIO to true instead, configure AllowSynchronousIO = true on your server options (KestrelServerOptions, IISServerOptions, or HttpSysOptions) in Program.cs, or enable it for a single endpoint via IHttpBodyControlFeature.
Updated November 2026: Modernized for modern ASP.NET Core (.NET 8 and .NET 9) using the
WebApplicationbuilder pattern, scoped per-request overrides withIHttpBodyControlFeature, and asynchronous streaming migration withSystem.Text.Json.
Over the last year, I’ve been dabbling in the dark arts of GraphQL. As a web API interface, it’s easy to consume and use, but I consider myself a back-end developer first and foremost. I have been spending my time learning how to build a GraphQL server implementation utilizing .NET Core with GraphQL.NET. I pulled down the project’s recent code, launched the harness project, and immediately hit an ASP.NET Core error:
System.InvalidOperationException: Synchronous operations are disallowed. Call ReadAsync or set AllowSynchronousIO to true instead. at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpRequestStream.Read(Byte[] buffer, Int32 offset, Int32 count) at System.IO.StreamReader.ReadBuffer(Span`1 userBuffer, Boolean& readToUserBuffer) at System.IO.StreamReader.ReadSpan(Span`1 buffer) at System.IO.StreamReader.Read(Char[] buffer, Int32 index, Int32 count) at Newtonsoft.Json.JsonTextReader.ReadData(Boolean append, Int32 charsRequired) at Newtonsoft.Json.JsonTextReader.ParseValue() at Newtonsoft.Json.JsonReader.ReadAndMoveToContent() at Newtonsoft.Json.JsonReader.ReadForType(JsonContract contract, Boolean hasConverter) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent) at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType) at Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader, Type objectType) at Newtonsoft.Json.JsonSerializer.Deserialize[T](JsonReader reader) at Example.GraphQLMiddleware.Deserialize[T](Stream s) in /Users/khalidabuhakmeh/Projects/dotnet/graphql-dotnet/src/GraphQL.Harness/GraphQLMiddleware.cs:line 93 at Example.GraphQLMiddleware.ExecuteAsync(HttpContext context, ISchema schema) in /Users/khalidabuhakmeh/Projects/dotnet/graphql-dotnet/src/GraphQL.Harness/GraphQLMiddleware.cs:line 54 at Example.GraphQLMiddleware.Invoke(HttpContext context, ISchema schema) in /Users/khalidabuhakmeh/Projects/dotnet/graphql-dotnet/src/GraphQL.Harness/GraphQLMiddleware.cs:line 41 at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)It turns out, starting in .NET Core 3.0 and continuing through modern .NET (.NET 8 and .NET 9), all synchronous operations on request and response bodies are turned off by default.
AllowSynchronousIO is an option in each server that enables or disables sync IO APIs like HttpReqeuest.Body.Read, HttpResponse.Body.Write, Stream.Flush, etc.. These APIs have long been a source of thread starvation and application hangs. Starting in 3.0.0-preview3 these are disabled by default. –Chris Ross
If you see this error after upgrading your project to .NET Core 3.0 or working with legacy serialization dependencies, you’re likely running on one of these .NET server implementations:
- Kestrel
- HttpSys
- IIS in-process
- TestServer
Errors can revolve around ReadAsync, WriteAsync, and FlushAsync with outputs similar to what is listed below:
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.In this post, we’ll explain why ASP.NET Core disables synchronous I/O by default, how to safely configure AllowSynchronousIO globally or per-request, and how to permanently resolve the issue by migrating to non-blocking asynchronous APIs.
Why Are Synchronous Operations Disallowed?
The root problem is thread pool starvation:
- Synchronous waiting blocks worker threads: When your application calls synchronous methods like
stream.Read()orreader.ReadToEnd()onHttpRequest.Body, the calling thread halts execution while waiting for raw network packets to arrive from the client. - Network latency is slow: While memory operations finish in nanoseconds, network I/O takes milliseconds or seconds. Holding onto a .NET thread pool thread during that interval leaves it idle.
- Thread exhaustion under load: When multiple concurrent requests block their assigned threads on synchronous I/O, the .NET thread pool exhausts its available workers. The runtime can only inject new threads gradually (~1 to 2 threads per 500ms once saturated). As a result, incoming requests back up in the queue, response latencies spike exponentially, and the service can become unresponsive or fail health check probes.
To prevent thread starvation, ASP.NET Core throws InvalidOperationException whenever synchronous read (Read), write (Write), or flush (Flush) operations occur on HTTP streams.
Quick Fix: Enable AllowSynchronousIO Globally
If you are maintaining legacy code or waiting on a third-party package update, you can globally re-enable synchronous I/O in your Program.cs file.
Modern .NET 8 / .NET 9 (WebApplication Builder)
Configure the server options for whichever web server host your application targets:
var builder = WebApplication.CreateBuilder(args);
// For Kestrel (default cross-platform server):builder.Services.Configure<KestrelServerOptions>(options =>{ options.AllowSynchronousIO = true;});
// For IIS In-Process:builder.Services.Configure<IISServerOptions>(options =>{ options.AllowSynchronousIO = true;});
// For HTTP.sys (Windows native server):builder.Services.Configure<HttpSysOptions>(options =>{ options.AllowSynchronousIO = true;});
var app = builder.Build();
app.MapControllers();app.Run();Legacy Startup.cs (.NET Core 3.x / .NET 5)
If you are maintaining a project that still uses the Startup class, add the configuration inside ConfigureServices:
public void ConfigureServices(IServiceCollection services){ services.Configure<KestrelServerOptions>(options => { options.AllowSynchronousIO = true; });
services.Configure<IISServerOptions>(options => { options.AllowSynchronousIO = true; });
services.AddControllers();}Better Alternative: Enable AllowSynchronousIO Per Request
Enabling AllowSynchronousIO globally exposes your entire web server to thread starvation risks. A significantly safer approach is to enable synchronous I/O only for the specific middleware or endpoint that needs it, leaving the rest of your application protected.
ASP.NET Core exposes the IHttpBodyControlFeature on HttpContext.Features:
Enabling in Middleware
Wrap legacy middleware or pipeline sections with a scoped feature override:
app.Use(async (context, next) =>{ // Check if the current request is for the legacy endpoint if (context.Request.Path.StartsWithSegments("/legacy-api")) { var syncIoFeature = context.Features.Get<IHttpBodyControlFeature>(); if (syncIoFeature is not null) { syncIoFeature.AllowSynchronousIO = true; } }
await next(context);});Enabling in an Action or Minimal API Endpoint
You can also opt in directly inside an individual controller action or Minimal API route:
app.MapPost("/process-legacy-payload", (HttpContext context) =>{ var syncIoFeature = context.Features.Get<IHttpBodyControlFeature>(); if (syncIoFeature is not null) { syncIoFeature.AllowSynchronousIO = true; }
// Call legacy library that performs synchronous stream reads using var reader = new StreamReader(context.Request.Body); var content = reader.ReadToEnd();
return Results.Ok(new { processed = true });});Using IHttpBodyControlFeature isolates the synchronous penalty exclusively to the callers who truly require it, without disabling safety checks across your high-performance routes.
The Permanent Solution: Migrate to Asynchronous I/O
The optimal fix is to update your code to perform all I/O asynchronously. Here are the most common migration patterns:
1. Reading Request Body as Text
Avoid synchronous StreamReader.ReadToEnd():
// ❌ BAD: Blocks thread pool threadusing var reader = new StreamReader(context.Request.Body);var body = reader.ReadToEnd();
// ✅ GOOD: Asynchronously reads stream without blockingusing var reader = new StreamReader(context.Request.Body);var body = await reader.ReadToEndAsync(context.RequestAborted);2. Deserializing JSON from Request Streams
Older serializers like Newtonsoft.Json defaulted to synchronous stream consumption. Modern .NET provides high-performance asynchronous JSON deserialization out of the box via System.Text.Json:
// ❌ BAD: Synchronous deserialization reads HttpRequest.Body synchronouslyvar data = JsonSerializer.Deserialize<Payload>(context.Request.Body);
// ✅ GOOD: Asynchronously streams and deserializes JSONvar data = await JsonSerializer.DeserializeAsync<Payload>( context.Request.Body, cancellationToken: context.RequestAborted);If you encounter serialization quirks when migrating between libraries, check out our guide on solving .NET JSON deserialization issues.
3. Writing Response Bodies
Always use the async write overloads when writing directly to HttpResponse.Body:
// ❌ BAD: Blocks thread pool threadcontext.Response.Body.Write(bytes, 0, bytes.Length);
// ✅ GOOD: Asynchronous non-blocking writeawait context.Response.Body.WriteAsync(bytes, 0, bytes.Length, context.RequestAborted);Server Configuration Summary
| Target Host | Configuration Type | Code Pattern |
|---|---|---|
| Kestrel (Global) | KestrelServerOptions |
builder.Services.Configure<KestrelServerOptions>(o => o.AllowSynchronousIO = true); |
| IIS In-Process (Global) | IISServerOptions |
builder.Services.Configure<IISServerOptions>(o => o.AllowSynchronousIO = true); |
| HTTP.sys (Global) | HttpSysOptions |
builder.Services.Configure<HttpSysOptions>(o => o.AllowSynchronousIO = true); |
| Single Request (Scoped) | IHttpBodyControlFeature |
context.Features.Get<IHttpBodyControlFeature>()!.AllowSynchronousIO = true; |
Frequently Asked Questions
What causes “Synchronous operations are disallowed” in ASP.NET Core?
The error occurs when an application or third-party dependency attempts to synchronously read from HttpRequest.Body (e.g. Stream.Read or StreamReader.ReadToEnd) or write to HttpResponse.Body (e.g. Stream.Write). Modern ASP.NET Core disables these synchronous APIs to prevent thread pool starvation.
Is AllowSynchronousIO safe to use in production?
It is safe as a temporary workaround, but it introduces thread pool starvation risks under heavy concurrent traffic. If you must use it in production, prefer scoping it per request with IHttpBodyControlFeature rather than enabling it globally across your entire server.
How do I enable AllowSynchronousIO for a single controller action?
Inside the controller action, retrieve IHttpBodyControlFeature from HttpContext.Features:
var feature = HttpContext.Features.Get<IHttpBodyControlFeature>();if (feature != null){ feature.AllowSynchronousIO = true;}Does AllowSynchronousIO affect System.Text.Json?
No, provided you use JsonSerializer.DeserializeAsync. However, if you call the synchronous JsonSerializer.Deserialize(Stream, ...) overload directly on context.Request.Body, it will throw InvalidOperationException unless AllowSynchronousIO is enabled.
Conclusion
It isn’t a great permanent workaround, but setting AllowSynchronousIO = true or scoping it with IHttpBodyControlFeature will keep you moving forward and unblock legacy dependencies. The better solution is to upgrade your libraries and perform all your actions asynchronously with ReadAsync, WriteAsync, and DeserializeAsync. I hope this helped!
References
Around the web
Comments
Loading comments from GitHub Discussions…