pact-foundation / pact-workshop-dotnet-core-v1

A workshop for Pact using .NET Core
Apache License 2.0
76 stars 209 forks source link

ProviderStateMiddleware Synchronous IO operation throws error #18

Open Issafalcon opened 4 years ago

Issafalcon commented 4 years ago

When targetting netcoreapp3.1 in the workshop project, using the prescribed Startup file from the workshop along with the ProviderStateMiddleware leads to an error at the following call:

jsonRequestBody = reader.ReadToEnd();

The error message is:

Microsoft.AspNetCore.Server.Kestrel: Error: Connection id "0HM3LKBBJTAHT", Request id "0HM3LKBBJTAHT:00000001": An unhandled exception was thrown by the application.

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 Waters.AuditLogAS.ContractTests.Middleware.ProviderStateMiddleware.HandleProviderStatesRequest(HttpContext context) in /home/ukafig/Repositories/auditlogas/ContractTests/Middleware/ProviderStateMiddleware.cs:line 47 at Waters.AuditLogAS.ContractTests.Middleware.ProviderStateMiddleware.Invoke(HttpContext context) in /home/ukafig/Repositories/auditlogas/ContractTests/Middleware/ProviderStateMiddleware.cs:line 28

The issue pertains to the following https://github.com/dotnet/aspnetcore/issues/7644.

The solution is to add the following to ConfigureServices:

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

I am happy to submit a PR with the above to ensure workshop code is compatible with .net core 3.1.