Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Synchronous operations are disallowed #649

Open
throwaway34059 opened this issue Mar 4, 2024 · 1 comment
Open

Synchronous operations are disallowed #649

throwaway34059 opened this issue Mar 4, 2024 · 1 comment

Comments

@throwaway34059
Copy link

throwaway34059 commented Mar 4, 2024

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.

The code:

public async Task<HttpResponseData> 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.¯_(ツ)_/¯.

@mvinicius2k
Copy link

Same here. Instead I use WriteAsync and Stream. Works...

//await response.WriteAsJsonAsync(responseDTO); <-- dont work
var serialized = JsonConvert.SerializeObject(responseDTO);
var ms = new MemoryStream(Encoding.UTF8.GetBytes(serialized));
await response.Body.WriteAsync(ms.ToArray());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants