我有一个自定义模型活页夹:
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;
}
}
使用大负载时, content.ReadAsStringAsync().Result 似乎使我的 Web 请求超时。
模型绑定器接口强制同步 API ......但是通过将此代码移动到我的控制器中:
public async Task<IHttpActionResult> DoStuff()
{
var json = await Request.Content.ReadAsStringAsync();
......
}
并使用 await over .Result 进行消费 - Web 请求毫无问题地通过。我很好奇为什么?