c# - Async/Await call doesn't return. Forcing sync does -
i've got problem async/await can't head around.
the code below within webapi controller:
this works ( forcing sync through .result )
public async task<httpresponsemessage> get(long id) { var distribution = this.service.getbyid(id).result; var result = distribution == null ? request.createresponse(httpstatuscode.notfound) : request.createresponse( httpstatuscode.ok, distribution.asviewmodel(identity)); homecoming result; } this doesn't ( using await )
public async task<httpresponsemessage> get(long id) { var distribution = await this.service.getbyid(id); var result = distribution == null ? request.createresponse(httpstatuscode.notfound) : request.createresponse( httpstatuscode.ok, distribution.asviewmodel(identity)); homecoming result; } here few observations , information:
the service talks repository on ef6 , gets stuff out of databaseasync ( .singleordefaultasync() ). the angular service fires request , remains pending in network tab. if navigate page, nothing. other times don't reach line after await , nil happens. no exceptions either. if debug service (having await keyword in place) , step through code 'just works' , info database , good. service , datacontext injected using ninject's inrequestscope() and weirdest thing have sworn worked when released lastly sprint. ideas how can go fixing thing?
edit
here's service:
public task<distribution> getbyid(long id) { homecoming this._distributionrepository.getbyid(id); } and here's repository:
public task<distribution> getbyid(long id) { homecoming this.datacontext.distributions.singleasync(d => d.id == id); }
as mentioned in comments, await on this.service.getbyid(id) should declare getbyid method async, , all way downwards should async. code should this
controller
public async task<httpresponsemessage> get(long id) { var distribution = await this.service.getbyidasync(id); var result = distribution == null ? request.createresponse(httpstatuscode.notfound) : request.createresponse( httpstatuscode.ok, distribution.asviewmodel(identity)); homecoming result; } service
public async task<distribution> getbyidasync(long id) { homecoming await this._distributionrepository.getbyidasync(id); } repository
public async task<distribution> getbyidasync(long id) { homecoming await this.datacontext.distributions.singleasync(d => d.id == id); } i suggest follow async method convention , add together async async methods, should rename getbyid method getbyidasync in service , repository.
c# entity-framework asynchronous async-await web-api
No comments:
Post a Comment