c# - Whats the overhead of await without I/O -
one downside of async pattern in c# 5 tasks not covariant, i.e there no itask<out tresult>
i have noticed devs do
return await someasyncmethod();
to come around this.
exactly impact performance wise crate? there no i/o or thread yield await , cast correct covariant, async framework under hood in case? there thread context switch?
edit: more info, code wont compile
public class listproductsqueryhandler : iqueryhandler<listproductsquery, ienumerable<product>> { private readonly ibusinesscontext _context; public listproductsqueryhandler(ibusinesscontext context) { _context = context; } public task<ienumerable<product>> handle(listproductsquery query) { return _context.dbset<product>().tolistasync(); } }
beacuse task not covariant adding await , cast correct ienumerable instead of list tolistasync returns
configureawait(false)
everywhere in domain code not feel viable solution use lowlever methods like
public async task<object> invoke(query query) { var dtotype = query.gettype(); var resulttype = getresulttype(dtotype.basetype); var handler = _container.getinstance(typeof(iqueryhandler<,>).makegenerictype(dtotype, resulttype)) dynamic; return await handler.handle(query dynamic).configureawait(false); }
the more notable cost of approach solving problem if there value in synchronizationcontext.current
need post value , wait schedule work. if context busy doing other work, waiting time when don't actually need in context.
that can avoided using configureawait(false)
, while still keeping method async
.
once you've removed possibility of using sync context, state machine generated async
method shouldn't have overhead that's notably higher you'd need provide when adding continuation explicitly.
Comments
Post a Comment