chahid khalil chahid khalil - 21 days ago 7
Android Question

Call webservice asynchronously

I need to call a web service so I added the webservice as web reference and I am able to call it. Now I need to make that call async because it need to download lot of data and it is taking lot of time. I tried to use the async/await method but I can not do this since the method in the webservice are not async.

Original code:

public static void validateLogin(JsonParameters _param, ref ValidateCredentials result, ref string excep_error)
{
var _mobileService = new MobileService();
_mobileService.Url = AppParam.IISSTRING + "/UrlAddressReference";
try
{
result = _mobileService.downloaddata(_param);
}
catch (Exception ex)
{
excep_error = ex.Message;
}

}


I tried to add async task in place of void to make the method async, but how can I await the webservice response?

After I added the webservice as a web reference I can see that it created for each method in it an async method and completed event. How can I make use of them? (Ex:
downloaddata", a method name
downloaddataasync`, and an event "downloaddatacompleted")

Is there a way to do this or do I need to change the webservice?

Answer
public async static Task validateLogin(JsonParameters _param, ref ValidateCredentials result, ref string excep_error)
        {
          await Task.Run(()=>
          {
            var _mobileService = new MobileService();
            _mobileService.Url = AppParam.IISSTRING + "/UrlAddressReference";
            try
            {
                result = _mobileService.downloaddata(_param);
            }
            catch (Exception ex)
            {
                excep_error = ex.Message;
            }
         });
}