title: Windows Phone 7: web service problem
body: <p>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.</p>
  
  <p>IMHO I respected the sample code at 100%, can anyone see where things go wrong? The code is below, thank you for your time:</p>
  
  <pre><code>    public partial class MainPage : PhoneApplicationPage
  {
      // Constructor
      public MainPage()
      {
          InitializeComponent();
      }
  
      /// &lt;summary&gt;
      /// Event handler to handle when this page is navigated to
      /// &lt;/summary&gt;
      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;
      }
  }
  </code></pre>
  
  <p>.</p>
  
  <pre><code>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
  
      /// &lt;summary&gt;
      /// Raise the PropertyChanged event and pass along the property that changed
      /// &lt;/summary&gt;
      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;
          }
      }
  
      /// &lt;summary&gt;
      /// Handle the information returned from the async request
      /// &lt;/summary&gt;
      /// &lt;param name="asyncResult"&gt;&lt;/param&gt;
      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(() =&gt;
              {
                  Counter = currentCount;
              });
          }
          catch (FormatException ex)
          {
              throw ex;
          }
  
      }
  }
  
  /// &lt;summary&gt;
  /// State information for our BeginGetResponse async call
  /// &lt;/summary&gt;
  public class CounterUpdateState
  {
      public HttpWebRequest AsyncRequest { get; set; }
      public HttpWebResponse AsyncResponse { get; set; }
  }
  </code></pre>