Azure / Azure/azure-functions-openapi-extension

Synchronous operations are disallowed

Open
#649 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
388
Forks
202
PR merge metrics
No merged PRs in 30d

Description

**Describe the issue**
.net 8 Azure Function in program.cs when configuring .ConfigureFunctionsWebApplication(worker => worker.UseNewtonSoftJson()) I will get a an error "Synchronous operations are disallowed. Call WriteAsync or set AllowSynchronousIO to true instead"

That led me to this [link](https://github.com/dotnet/aspnetcore/issues/8302).

The code:
```
public async Task Get([HttpTrigger(AuthorizationLevel.Function, "get", Route = "v1/user")] HttpRequestData req)
{
...
var response = req.CreateResponse(HttpStatusCode.OK);
await response.WriteAsJsonAsync(user);
return response;
```
}

It will bomb out on .WriteAsJsonAsync() from swagger and postman. It could be my object I am returning but..

**My work around**:

My work around was to remove worker => worker.UseNewtonSoftJson() from my config.

```
var host = new HostBuilder()
.ConfigureFunctionsWebApplication() //Note: OpenApi says to configure this as worker => worker.UseNewtonSoftJson(). This will cause a Synchronous operations are disallowed. Call WriteAsync or set AllowSynchronousIO to true instead. I removed it and things seem to work?
.ConfigureServices((hostContext, services) =>
...
```

This works for swagger and json. It could be the object I am serializing that doesn't play nice with NewtonSoft. idk. The link make it seem like there's some other issue with flushing on write.¯\_(ツ)_/¯.

Contributor guide

Open the contributing guide

Research direction

Start with the Program.cs configuration using ConfigureFunctionsWebApplication and worker.UseNewtonSoftJson(), then reproduce the failure at response.WriteAsJsonAsync() with the HttpTrigger example. Compare behavior with UseNewtonSoftJson() removed and identify whether serialization or response flushing triggers the synchronous-I/O error; done means the configured Newtonsoft path works for Swagger and JSON responses.

Written by the indexing model from the issue text.

Assessment

Tech stack
azure, csharp, openapi
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.