c# - How can I do async await in the repository pattern? -
i have code long running i/o bound perfect async/await . doing repository pattern , can not figure out how await in controller i'm getting object not contain awaiter method on code
public class homecontroller : controller { private imainrepository _helper = null; public homecontroller() { this._helper = new mainrepository(); } public async task<string> aboutt() { // here error object main = await _helper.top_five() ?? null; if(main != null) { return main.tostring(); } else { return null; } } }
the method implementing works fine can see below. data out of database , return in string format. find way make object main = await _helper.top_five() ?? null; await otherwise mixing async synchronous code. suggestions great ...
public async task<string> top_five() { try { using (npgsqlconnection conn = new npgsqlconnection("config")) { conn.open(); string results = null; npgsqlcommand cmd = new npgsqlcommand("select * streams limit 15)t", conn); using (var reader = await cmd.executereaderasync()) { while (reader.read()) { results = reader.getstring(0); } return results; } } } catch(exception e) { // log here return null; } }
does work you?
object main = (await _helper.top_five()) ?? null;
notice (
, )
since need await method , check null
.
Comments
Post a Comment