views:

115

answers:

1

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; }
}
+1  A: 

Your sample code wouldn't even compile... compare this:

Globals.pizzaCounter = searchService.GetCategoryCounter("pizza");

with this:

public void GetCategoryCounter(string category)

Note how it's a void method - so you can't assign the return value to Globals.pizzaCounter.

You need to bear in mind that this happens asynchronously. As you said, "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." That's the whole meaning of it being asynchronous. You don't want to block your UI thread waiting for the service to return.

Instead, you should pass your service fetching code a callback to execute when your service has returned some data. That will then need to marshal back to the UI thread via the Dispatcher, and then it can update the textbox. Your code already does some of this, by marshalling back to the UI thread and then updating the property, which will raise the PropertyChanged event, letting the UI know to refetch the data from the property. However, it looks like there's only one counter, whereas you're trying to fetch two different categories. You should probably separate out the service itself from a "category counter" (or whatever) which knows about the search service, but also is specific to one category. You'd then have two instances of this (but only one search service); each textbox would bind to the relevant instance of the category counter.

Jon Skeet
My bad, I tried several possibilities and the code I posted was a hybrid... It's corrected now. BUT the problem remains. I have a callback, I have a dispatcher, the PropertyChanged event is raised... There is only one Counter because it is meant to be like this (there are several queries of the web service with different values for the "category" parameter). Any other suggestion...?
Manu
@Manu: Why would you only have one Counter when you're making different queries at the same time? How do you expect your two queries to work? Frankly the name "Globals" is a bit of a warning sign that things aren't quite right. But your code is still assigning a value based on Counter immediately after you've *launched* the request, which isn't what you need to do. You need to *bind* your textbox to Counter, so that when the response is received and the value of Counter changes, your UI will be updated.
Jon Skeet
Thank you for your patience, Jon. I need to do it this way, since the web service is not mine and can not change it. It receives a category as input and provides a counter as output. I need to collect all the counters for all the categories in my app, only the first time: on app launch. After that, I need to retrieve the same counters every time I come back to the main page, but WITHOUT re-querying the web service (that's the use of the Global vars). So there is no textbox to bind, I need to store the counters into some string variables and I don't succeed to do that. I'm stuck.
Manu
@Manu: How the web service works is irrelevant to how you use it. You're making two calls, you presumably want access to both of those values independently, so you'll need more than one instance of `Counter`. (And I would still argue against using globals like this.) If you're not binding to a textbox, what is `pizzacounter.Text = Globals.pizzaCounter;` doing?
Jon Skeet
"If you're not binding to a textbox, what is pizzacounter.Text = Globals.pizzaCounter; doing?" - I bind pizzacounter.Text with a value that I previously obtained from the web service. If later on I navigate through my app and return 20 times to the main page, I don't want to query the web service each time, but I want to use the same counters I got on application launch. In fact, pizzacounter.text displays a static text, that value is susceptible to change only if the user terminates the application and relaunches it.
Manu
@Manu: I'm fine with the fact that you need state - but using global variables for this doesn't sound like a great idea to me. Why not use PhoneApplicationPage.State for that? Either way, it doesn't address the fact that you're making an asynchronous call, but expecting the data to be present immediately after you've *started* the call. It just doesn't work like that. If you create two counters and bind the textblocks to those counters, they will be populated when the web service returns. I strongly suggest you get *that* working first, and worry about caching afterwards.
Jon Skeet
Thank you Jon, I'll try binding the counters to the text boxes, anyway this is far more important than caching. I am very grateful for your patience in guiding me.
Manu