c# - How does waiting on Task.Result affect nested async/await methods? -
this bit long winded, here goes....
given have interface so:
public interface iwebclienthelper { tpayload get<tpayload>(string url); }
where implementation of get
makes call supplied url, return response containing json object of type tpayload
), , json deserialized tpayload
, returned.
i make implementation of get
method asynchronous (or more make http call contained within get
method asynchronous), understand it, require signature of get
method changed to:
task<tpayload> get<tpayload>(string url);
i aiming keep interface is, created second interface:
public interface iasyncwebclienthelper { task<tpayload> get<tpayload>(string url); }
and injected implementation of iwebclienthelper
. implementation of iwebclienthelper
looks this:
public tpayload get<tpayload>(string url) { return _asyncwebclienthelper.get<tpayload>(url).result; }
and get
method of _asyncwebclienthelper
contains line
message = await httpclient.getasync(url);
so unclear on this: correct in thinking line return _asyncwebclienthelper.get<tpayload>(url).result
block execution until method returns? or await
keyword inside method release thread until has received response url?
yes, using result
means method block. however, it's quite possible means cause deadlock. haven't told context, if you're in context after await
need return same thread, thread blocked due result
, you're deadlocking on yourself. need very careful use blocking calls such result
property or wait()
method.
fundamentally, trying use asynchrony without making interface asynchronous tricky/pointless. you'd better off embracing asynchrony whole-heartedly, or sticking synchronous version. after all, if you're going keep thread blocked until asynchronous task has completed, what's benefit of asynchrony in first place?
Comments
Post a Comment