From a WindowsPhone7 application I need to query a web service by sending a "category" string parameter an expecting to get a string in return. I tried to follow identically the "Weather Forecast" sample from the MSDN, but I always get en empty string. If I add some Debug.WriteLine commands I can see that the callback executes AFTER the response is returned, that is: the return doesn't wait for the asynchronous operation to end.
IMHO I respected the sample code at 100%, can anyone see where things go wrong? The code is below, thank you for your time:
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}
/// <summary>
/// Event handler to handle when this page is navigated to
/// </summary>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
SearchService searchService = new SearchService();
searchService.GetCategoryCounter("pizza")
Globals.pizzaCounter = searchService.Counter;
searchService.GetCategoryCounter("pasta")
Globals.pastaCounter = searchService.Counter;
pizzacounter.Text = Globals.pizzaCounter;
pastacounter.Text = Globals.pastaCounter;
}
}
.
public class SearchService
{
#region member variables
private string currentCategoryCount = "";
public event PropertyChangedEventHandler PropertyChanged;
#endregion member variables
#region accessors
public String Counter
{
get
{
return currentCategoryCount;
}
set
{
if (value != currentCategoryCount)
{
currentCategoryCount = value;
NotifyPropertyChanged("Counter");
}
}
}
#endregion accessors
#region private Helpers
/// <summary>
/// Raise the PropertyChanged event and pass along the property that changed
/// </summary>
private void NotifyPropertyChanged(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
#endregion private Helpers
/*
* GetCategoryCounter Method:
*
* @param category
* : the category whose counter is to be retrieved
*/
public void GetCategoryCounter(string category)
{
try
{
// form the URI
UriBuilder fullUri = new UriBuilder("http://www.mywebsite.com/items/stats");
fullUri.Query = "category=" + category;
// initialize a new WebRequest
HttpWebRequest counterRequest = (HttpWebRequest)WebRequest.Create(fullUri.Uri);
// set up the state object for the async request
CounterUpdateState counterState = new CounterUpdateState();
counterState.AsyncRequest = counterRequest;
// start the asynchronous request
counterRequest.BeginGetResponse(new AsyncCallback(HandleCounterResponse), counterState);
return;
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// Handle the information returned from the async request
/// </summary>
/// <param name="asyncResult"></param>
private void HandleCounterResponse(IAsyncResult asyncResult)
{
// get the state information
CounterUpdateState counterState = (CounterUpdateState)asyncResult.AsyncState;
HttpWebRequest counterRequest = (HttpWebRequest)counterState.AsyncRequest;
// end the async request
counterState.AsyncResponse = (HttpWebResponse)counterRequest.EndGetResponse(asyncResult);
Stream streamResult;
string currentCount = "";
try
{
// get the stream containing the response from the async call
streamResult = counterState.AsyncResponse.GetResponseStream();
StreamReader reader = new StreamReader(streamResult);
currentCount = reader.ReadToEnd();
// copy the data over
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
Counter = currentCount;
});
}
catch (FormatException ex)
{
throw ex;
}
}
}
/// <summary>
/// State information for our BeginGetResponse async call
/// </summary>
public class CounterUpdateState
{
public HttpWebRequest AsyncRequest { get; set; }
public HttpWebResponse AsyncResponse { get; set; }
}