c# - Async request never ends using RestSharp -
i'm using restsharp request asynchronously never ends. if same request synchronously ends successfully. have used restsharp before , worked.
what doing wrong?
here's async method: (this.client
localhost)
public async task<serviceresponse> upvotearticleasync(string source, string url) { var client = this.client; var request = new restrequest("service/upvote/"+source+"/"+url, method.get) { requestformat = dataformat.json }; var cancellationtokensource = new cancellationtokensource(); var deserial = new jsondeserializer(); var response = await client.executetaskasync(request, cancellationtokensource.token); return deserial.deserialize<serviceresponse>(response); }
and how call it:
serviceresponse result = await rest.upvotearticleasync (this.article.source, this.article.url);
as said if change async method sync one, works:
public async task<serviceresponse> upvotearticleasync(string source, string url) { var client = this.client; var request = new restrequest("service/upvote/"+source+"/"+url, method.get) { requestformat = dataformat.json }; var cancellationtokensource = new cancellationtokensource(); var deserial = new jsondeserializer(); var response = client.executetaskasync(request, cancellationtokensource.token); return deserial.deserialize<serviceresponse>(response.result); }
edit:
i used try , catch see if there's exceptions didn't throw any.
edit 2:
even if same request httpwebrequest
system.net
still gets stuck when using async version of getresponse
:
public async task<bool> upvotearticleasync(string source, string url) { var request = httpwebrequest.create(string.format(@"http://192.168.43.199:8080/service/upvote/{0}/{1}", source, url)); request.contenttype = "application/json"; request.method = "get"; using (httpwebresponse response = await request.getresponseasync () httpwebresponse) { if (response.statuscode != httpstatuscode.ok) console.out.writeline("error fetching data. server returned status code: {0}", response.statuscode); using (streamreader reader = new streamreader(response.getresponsestream())) { var content = reader.readtoend(); if(string.isnullorwhitespace(content)) { console.out.writeline("response contained empty body..."); } else { console.out.writeline("response body: \r\n {0}", content); } } } return true; }
Comments
Post a Comment