views:

25

answers:

2

Hello, I have a webservice that calls a method and returns a generic list. The webservice completed method looks like this (note: names and e.Result are both of the same type of List):

   void SetNames()
   {    
        ServiceReference1.ServiceClient webservice = new ServiceReference1.ServiceClient();

        webservice.GetNameCompleted += new EventHandler<GetNameCompletedEventArgs>(webservice_GetNameCompleted);
        webservice.GetNameAsync();
    }
    private void webservice_GetNameCompleted(object sender, ServiceReference1.GetNameCompletedEventArgs e)
    {
       names = e.Result;     
    }

The problem I'm having is that I can only retrieve the items in the names list in the webservice method. Whenever I try to access the items in the names list anywhere outside of that method it is empty. For example (this displays nothing in the textbox),

List<string> names = new List<string>();

public MainPage()
{
    InitializeComponent();
    SetNames();

    foreach (string name in names)
        textBox1.Text += name;
 }

But this will display the correct thing:

private void webservice_GetNameCompleted(object sender, ServiceReference1.GetNameCompletedEventArgs e)
    {
       names = e.Result;

       foreach (string name in names)
          textBox1.Text += name;     
    }

I'm new to Silverlight and webservies, and I'm probably over looking something. I've been working on this for a while and I'm at the point where I feel I need to ask for help. Any help would be greatly appreciated!

+3  A: 

In Silverlight all calls to web-services are asynchronous (unlike WPF which can also use synchronous call).

It means that the code after the call to the web-service will be invoked before the service has sent a response to the Silverlight client.

So, in the MainPage constructor, the foreach loop is iterating over the collection BEFORE the service has returned, and then iterate over an empty collection.

The right way to proceed is the second one : initializing the collection after the service has responded, in the callback method dedicated to this task : webservice_GetNameCompleted.

Serious
Thanks. I did not know exactly what was was going on with the webservice, but I knew it had something to do with the service. Thanks for the knowledge.
Lisa
A: 

You have to wait for the Web Servicec call back to complete. By defualt all Silverlight WCF web service calls are asynchronous. you are sending a request to the webservice and unlike .asmx with WCF and Silverlight the application continues to run instead of waiting for the webservice to return a result.

So when you make a call like:

public MainPage() 

{

InitializeComponent(); 
SetNames(); 

foreach (string name in names) 
    textBox1.Text += name; 

}

The application does not stop and wait for SetNames to Return a value it just carries on and since the webservice hasn't returned a result yet you have a blank or null list still when you call your foreach.

Cheers

Anthony
I greatly appreciate this bit of info as well. It's good to know step by step what's going on. This will definitely help me progress with the project.
Lisa