views:

250

answers:

2

The webservice that I am calling from my application has two methods.

  1. XmlNode getCase(string parameter) // synchronous
  2. void getCaseAsync(string parameter) //async

I can simply call method 1 and store the results in an xmlnode like this,

XmlNode node=webservice.getCase("test");

but I can not figure out how to get the result back from the async method returning void. I tried this but get an erorr:

IAsyncResult result = webservice.getCaseAsync(("test");

Any ideas?

Yes Brian you are right there is a "completed" event,that I already have implemented in my Form consturcor class like this,

webService.getCaseCompleted += new webService.getCaseCompletedEventHandler(webService_getCaseCompleted);

void webService_getCaseCompleted(object sender,webService.getCaseCompletedEventArgs e) { webService.GetCaseAsync("test"); } I also have a button on my form which I want to run the code from there.I tried this, private void button1_Click(object sender, EventArgs e) { webService_getCaseCompleted(this, null); } But I get error that "e" is Null.How should I run this methode?

Thanks, Dave

+1  A: 

If method return type is void it means it doesn't return anything. If it doesn't return anything then you cannot expect any result from it, that's why your assignment is invalid. You should just call such methods like that:

webservice.getCaseAsync("test");

If you want it to return something modify the method return type to something other then void.

RaYell
+3  A: 

Assuming you are talking about a WCF proxy, it would go like this:

My IService would look something like this:

[ServiceContract]
public interface IService
{
    [OperationContract]
    void DoSomething();
}

In your client, your code would look like this:

var serviceProxy = new MyService.Service1Client();
serviceProxy.DoSomethingCompleted += DoSomethingComplete;
serviceProxy.DoSomethingAsync();

And your asynchronous callback would look like this:

private void DoSomethingComplete(object sender, AsyncCompletedEventArgs e)
{
    // Handle the result
}

In the completed handler, you can check to see if the service call succeeded (e.Cancelled == false and e.Error == null).

Remember. An asynchronous call will not give you a result immediately. It will tell you when it is complete.

Hope this helps?

Brian Genisio
Obviously there has to be some method of calling the web service to find out if the async method has completed and, if so, to get the result. Not knowing the API it might be this or it might be some other, less sensible, way...
Will