-1

I have a custom model binder:

public class JsonPolyModelBinder : IModelBinder
{
    private readonly JsonSerializerSettings settings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto };

    public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
    {
        var content = actionContext.Request.Content;
        var json = content.ReadAsStringAsync().Result;
        var obj = JsonConvert.DeserializeObject(json, bindingContext.ModelType, settings);
        bindingContext.Model = obj;
        return true;
    }
}

With large payload, content.ReadAsStringAsync().Result seems to timeout my web requests.

The model binder interface forces synchronous API's...but by moving this code into my controller:

public async Task<IHttpActionResult> DoStuff()
{
    var json = await Request.Content.ReadAsStringAsync();
     ......
}

And consuming using await over .Result - the web requests go through without a problem. I am curious as to why?

user3689167
  • 863
  • 1
  • 14
  • 28

1 Answers1

0

if you use Result in Task it would block the current executing thread until it returns the result , If You await the call the request runs asynchronously without blocking the thread and you can run multiple request (Tasks) functions using await without blocking the thread. so it is better to use await than result.

Usman Khalid
  • 140
  • 1
  • 8