我有两个方法,一个在WebApi(Post()),一个在我的数据仓库(Save()).在Save方法中,我用await调用异步方法.Save方法本身是异步的.
我最终想要完成的是在Save方法中的函数完成后向用户发送201.
Web api:
public HttpResponseMessage Post(JObject input)
{
Event postedEvent = new Event(// here be data //);
IEventRepo repo = new MongoDBRepo();
return repo.Save(postedEvent).Result;
}
Run Code Online (Sandbox Code Playgroud)
数据回购:
public async Task<HttpResponseMessage> Save(Event e)
{
await _collection.InsertOneAsync(e);
return new HttpResponseMessage(HttpStatusCode.Created);
}
Run Code Online (Sandbox Code Playgroud)
现在发生的是Save将完成,但HttpResponseMessage永远不会被发送.因此对服务器的请求将挂起.