bartschotten/com-pact

Failed to call MockProvider due to synchronized I/O call

RotemKir opened this issue · 2 comments

When trying to call the mock provider (for api consumer tests) I get the following 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()
at System.IO.StreamReader.ReadToEnd()
at ComPact.Models.V3.Request..ctor(HttpRequest request)
at ComPact.MockProvider.RequestResponseMatcher.MatchRequestAndReturnResponseAsync(HttpRequest httpRequest, HttpResponse httpResponseToReturn)
at ComPact.MockProvider.ProviderWebHost.<>c__DisplayClass0_0.<b__1>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication`1 application)

This is probably due to changes in .net core 3.0 that prohibit this behavior.

The problematic code is in the V3.Request ctor:

using (var streamReader = new StreamReader(request.Body, Encoding.UTF8))
{
                var serializedBody = streamReader.ReadToEnd();
                Body = JsonConvert.DeserializeObject<dynamic>(serializedBody);
}

The call to ReadToEnd is the culprit.
Can it be fixed to an async call ? Or set the AllowSynchronousIO to true on the WebHost ?

Thanks,
Rotem

Unfortunately I can't reproduce the error simply by setting the test project to .NET Core 3, but I'll push a new version (0.3.2) with AllowSynchronousIO. Let me know if that fixes it.

The fix works great (checked on 0.3.3), thanks for the quick response !