Carter: Missing async versions for Bind and BindAndValidate

From version 10.0 Newtonsoft.Json supports async serialization and deserialization from streams using JToken (and derived).

Would be useful to add the async versions for Bind<T> and BindAndValidate<T> extension methods, which will read the request body in a fully async manner:

var model = await req.BindAsync<MyModel>();
await res.Negotiate(model);

If interested I may send a PR.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 19 (14 by maintainers)

Most upvoted comments

Ah good spot! Thanks.

For asp.net core 3 we will probably move to the new json serializer that comes with it

On Tue, 16 Jul 2019 at 10:17, Stephen Pope notifications@github.com wrote:

This seemed the most logical place to put this … I can open up a seperate issue if needed.

I was starting to use Carter (4.2.0) with Netcore 3.0 Preview 6 (3.0.100-preview6-12264).

When trying to call BindAndValidate 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(Span1 userBuffer, Boolean& readToUserBuffer) at System.IO.StreamReader.ReadSpan(Span1 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 Carter.ModelBinding.BindExtensions.Bind[T](HttpRequest request) at Carter.ModelBinding.BindExtensions.BindAndValidate[T](HttpRequest request)

It seems to relate to this announcement -> aspnet/AspNetCore#7644 https://github.com/aspnet/AspNetCore/issues/7644 … that synchronousIO is disallowed by default in NetCore 3.

Adding :

var syncIOFeature = req.HttpContext.Features.Get<IHttpBodyControlFeature>();

if (syncIOFeature != null) { syncIOFeature.AllowSynchronousIO = true; }

… before calling it solves the issue. But it seems that BindAndValidate method is going to blow up by default from NetCore 3 onwards.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/CarterCommunity/Carter/issues/39?email_source=notifications&email_token=AAAZVJU4WTIQFAXBJYIEXVTP7WGY3A5CNFSM4D7HB4W2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD2AHS4Q#issuecomment-511736178, or mute the thread https://github.com/notifications/unsubscribe-auth/AAAZVJR32VCYGBNDMHJH6CLP7WGY3ANCNFSM4D7HB4WQ .

Memory stream is a bad idea that’s why we don’t do that 🙂.