c# - MongoCollection returns data sync but not async -


i've been trying convert sync logic async , realized async await pattern doesn't work.

i've changed code:

var filter = builders<smartagentproperty>.filter.where(smartagent => smartagent.usermail==usermail); var results = await smartagentscollection.findasync(filter); return results.tolist(); 

to this:

var filter = builders<smartagentproperty>.filter.where(smartagent => smartagent.usermail == usermail); var results = smartagentscollection.find(smartagent => smartagent.usermail == usermail).toenumerable(); return task.fromresult(results); 

the sync version works perfectly.

the async version hanging , doesn't throw exceptions.

as sounds, extremely wierd bug.

i thought might doing things wrong seems same pattern works in other places in code i'm reaching out help.

so based on craig's comment, issue solved!

a. using wrong (task.fromresult instead of actual async implementation)

b. missing configureawait(false)

c. should have used find(filter).tolistasync() instead of findasync(filter).toenumerable()

here's code after fixing it:

return await _smartagentscollection       .find(smartagent => smartagent.usermail == usermail)       .tolistasync().configureawait(false); 

Comments

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -