.net - Need some advice about the correct way to write own httpclient interface -
i want create wpf application connect web api server. i'm newbie in wpf. more worse,even in .net.
actually, things work, follow http://www.asp.net/web-api/overview/web-api-clients/calling-a-web-api-from-a-wpf-application.
after more search,i found totally work on wrong way(winform way).and seems should use mvvm style communites said. decide rewrite code.
i use prism create module, , bind name , password viewmodel class. can name , password textbox.
first, failed create httpclient instance every viewmodel use. below problem links
hot register constructed instance in unity?
so deicide write this. first create interface.
public interface idata { ienumerable<device> getdevicesbyuser(user user); ienumerable<user> getusers(); currentuser getcurrentuserinfo(); }
and implement interface
public class httpservice : idata { httpclient client = new httpclient(); public httpservice() { client.baseaddress = new uri("https://localhost:3721"); client.defaultrequestheaders.accept.add(new mediatypewithqualityheadervalue("application/json")); } public ienumerable<device> getdevicesbyuser(user user) { throw new notimplementedexception(); } public ienumerable<user> getusers() { throw new notimplementedexception(); } public currentuser getcurrentuserinfo() { throw new notimplementedexception(); } }
later, can register instance in unity, , can use in every viewmodel.but situation can't go further. use 2 method,but neither can works. need advice correct way.
the fisrt way. write async methods each interface method.
public currentuser getcurrentuserinfo() { return getcurrentuserinfofromserver(); } private async void getcurrentuserinfofromserver() { try { var response = await client.getasync("api/user"); response.ensuresuccessstatuscode(); var currentuser = await response.content.readasasync<currentuser>(); } catch (newtonsoft.json.jsonexception jex) { messagebox.show(jex.message); } catch (httprequestexception ex) { messagebox.show(ex.message); } { } }
but seems async can not return type other void , task. , seems weird. try write request 1 method.
private async void getdata(string requesturi) { try { var response = await client.getasync("api/user"+requesturi); response.ensuresuccessstatuscode(); var ? = await response.content.readasasync<?>(); } catch (newtonsoft.json.jsonexception jex) { messagebox.show(jex.message); } catch (httprequestexception ex) { messagebox.show(ex.message); } { } }
this codes still have same problem first one. can not return type other void , task, , problem how data dynamic?
async methods should return task
or task<t>
, because that's being asynchronous means: method returns immediately, hasn't completed yet. so, result, await
returned task
. so, method this:
private async task<currentuser> getcurrentuserinfofromserverasync() { try { var response = await client.getasync("api/user"); response.ensuresuccessstatuscode(); var currentuser = await response.content.readasasync<currentuser>(); return currentuser; // sets result of returned task } catch (newtonsoft.json.jsonexception jex) { messagebox.show(jex.message); } catch (httprequestexception ex) { messagebox.show(ex.message); } }
this means method calls getcurrentuserinfofromserverasync()
needs async
, , method that, etc., way top.
also, it's practice separate concerns: httpservice
should not show messabebox
es , instead let exceptions propagate out of method. should leave dealing ui higher layers.
Comments
Post a Comment