添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
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.