4

I have just started learning WP programming so this might be little silly question...

Im developing app which fetch data from one web services few different method. So I decided to put all this web service fetching code to 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 how I should call this from my "main app" code, im trying something like this:

WebApiWorker webApi = new WebApiWorker(); var geoResponse = await webApi.GetGeocodeAsync("address"); 

So whats wrong with this, i get the compiler error: The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'.

All suggestions are welcome.

3
  • 4
    What's wrong with your current attempt? Compiler error? Exception? Commented Jun 13, 2013 at 15:23
  • I get the compiler error, I edited the first post. Commented Jun 13, 2013 at 17:46
  • 1
    So, have you considered marking that method with the async modifier and changing its return type to Task? Commented Jun 13, 2013 at 18:06

2 Answers 2

4

Make sure your method in your "main app" code also has the "async" modifier:

public async Task GetAddress() { WebApiWorker webApi = new WebApiWorker(); var geoResponse = await webApi.GetGeocodeAsync("address"); } 
Sign up to request clarification or add additional context in comments.

Comments

0

The answer to this one slightly depends on what you want from the response. Are you going to use the result right away? If so, then you're not really using asynchronous functionality at all, and can just make another version of your function that is synchronous. If your calling function is meant to continue later, then do as Chris (and your compiler) said; make the calling function async.

It's true that eventually, a root caller will need to be NOT async; generally, that one will be a void method that doesn't return anything. (In fact, said void will return before your asynchronous functions do)

5 Comments

Whenever possible, the root callers should still be async. On the Windows Phone platform, a root caller is usually an async void event handler.
Ah; I have been trying to keep up with C# language features out of interest, but still don't have much of any Phone/WPF experience, so thank you for that.
Ok, so maybe I little mess up my app design, and the way how async and await works. Basically I try to get these geo locations using GetGeocodeAsync(..) and populate them to AutoCompleteBox. Right now Im calling this GetGeocodeAsync(...) from my AutoCompleteBox populating function, so I think i need to change this to async too.
The basic idea of Windows Phone's responsiveness (which reviewers always note) is that any calls coming from the UI thread return in less than a millisecond, and if they have to do any processing, do it asynchronously. So the button you click won't do anything right away, but 2 seconds later when that geocode comes in, a box will be populated. If that 2 seconds were in the UI thread, the UI would always be freezing for half-seconds to finish some action.
What is best way to handle this kind of (very common) situation. I have one class which handles all networking stuff, fetches data from web service. Now im making object from that class somewhere in my application MainPage methods. Should I make all methods from my root caller to that method in my networking / web service class to async? Is there any good tutorials from this subject for this kind of noob? :D

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.