相关文章推荐
英姿勃勃的葡萄  ·  成功解决TypeError: ...·  11 月前    · 
大力的馒头  ·  mybatis 利用 ...·  2 年前    · 
千杯不醉的豆腐  ·  freemarker.template.Te ...·  2 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams
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?

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.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.