views:

863

answers:

4

I have hosted a WCF service in windows service. I have console app for which I added a WCF service reference and generated Client for it.

I can make Sync call to the service,but Async call doesn't seem to work. If i attach server process it doesn't hit the service at all.

client= new ServiceClient();
client.DoSomething();//Works fine

client.DoSomethingAsync()//Doesnot work

Is this a known problem?

A: 

When you generated the client, did you tick the box to specify "Generate asynchronous operations"?

David M
yes i can see the Async methods in generated code
Deepak N
So just to be totally clear, this is the DoSomethingAsync method in the client that was auto-generated for the DoSomething method in the service itself? You haven't tried exposing a DoSomethingAsync method yourself in the service?
David M
yes..DoSomethingAsync method in the client that was auto-generated for the DoSomething method in the service
Deepak N
+1  A: 

The asynccall will probably be started in a background workerthread. So what could be happening is that your async thread is dieing out because the foreground thread has finished it's processing.

Unless you have some logic after you make this call to wait for the reponse, or continue with some other work on your main thread, the background thread may not have the time to get created before the application exits.

This can be easily tested by adding Thread.Sleep after the async call.

client.DoSomethingAsync();
Thread.Sleep(1000);

A symptom of this happening is that your service starts / stops unexpectedly and windows throws an error.

pavsaund
Thanks..it worked after adding Sleep
Deepak N
That's great! glad to be of help
pavsaund
A: 

From the code posted, i'm assuming you have not set up handlers to deal with the response from the async method. You'll need something like the example at the bottom of this msdn post where you use AddHanlder to handle the response.

Something like the below before you make the async call:

AddHandler client.DoSomethingCompleted, AddressOf DoSomethingCallback

With a method to deal with the outome:

Private Shared Sub DoSomethingCallback(ByVal sender As Object, ByVal e As DoSomethingCompletedEventArgs)

        'Do something with e.Result
        MsgBox e.Result

End Sub
Tanner
A: 

If you have a call to

client.DoSomethingAsync()//Doesnot work

then did you specify a handler for the callback completed event??

public event DoSomethingCompletedEventHandler DoSomethingCompleted;

What happens is that the async call goes off, but how would it send you back any results?? You'll need to provide a handler method for that - hook it up to the DoSomethingCompleted event handler! In that method, you'll get the results of the async call and you can do with them whatever you need to do.

Marc

marc_s