I just started learning WP programming, so this might be a bit of a dumb question ...
Im a development application that retrieves data from one web service in several different ways. So I decided to put this whole set of web services in one class:
class WebApiWorker { public async Task<List<GeocodeResponse>> GetGeocodeAsync(String address) { String url = "http://api.com&search=" + HttpUtility.UrlEncode(address) + "&format=json"; HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url); httpWebRequest.Method = "GET"; HttpWebResponse response = (HttpWebResponse) await httpWebRequest.GetResponseAsync(); Stream responseStream = response.GetResponseStream(); string data; using (var reader = new System.IO.StreamReader(responseStream)) { data = reader.ReadToEnd(); } responseStream.Close(); var geocodeResponse = JsonConvert.DeserializeObject<List<GeocodeResponse>>(data); return geocodeResponse; } }
But as I should call it from my โmain applicationโ, I try something like this:
WebApiWorker webApi = new WebApiWorker(); var geoResponse = await webApi.GetGeocodeAsync("address");
So what's wrong with this, I get a compiler error: The wait statement can only be used in an asynchronous method. Consider labeling this method with the async modifier and change its return type to Task.
All suggestions are welcome.
devha source share