views:

406

answers:

1

I created a server and generated my client as an asynchronous one.

So when I connect, I do so asynchronously. That's fine.

I have a method on my client (a callback method) that receives data from the server. The server sends this data using an asychronous method too.

To receive this data on my client, I expected to have ONLY a method like this one it generated for me:

public void SendToClient(string dataToSend)
{
}

Although it has a bizarre name, this is the method that I expected - it is called when data is received.

However it has also generated these methods:

public IAsyncResult BeginSendToClient(string dataToSend, AsyncCallback callback, object asyncState)
{
        throw new NotImplementedException();
}

public void EndSendToClient(IAsyncResult result)
{
    throw new NotImplementedException();
}

I don't know how to use them. It's like an asynchronous receive, or something. How would I use them to receive data?

+1  A: 

Generally you don't want callback contract operations to be asynchronous. You would be forced to implement the asynchronous programming model which is difficult to get right, and unnecessary in this case.

You should be able to get away with not implementing the asynchronous versions on the server side and just fill in the synchronous one. On the client side (the caller of the async callback contract), WCF should do the right thing, and behind the scenes your synchronous version will be called when the data arrives on your end.

As a sidenote, it seems that a customer reported this as a bug, but it looks like it did not get addressed: http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=293507

bobbymcr
Fair comment but it doesn't exactly answer the question...
Murph
It has to be asynchronous, otherwise if you try to callback to a client that has pulled the plug out of his PC, the server will freeze until the timeout is reached, which is not acceptable.
Simon
The workaround posted on that link would appear to be the solution - simply removing it from the contract.
Simon
Maybe I wasn't clear enough above, but I said that the caller of the async contract can use the async versions and your actual implementation can still be synchronous. There is a difference between client and server side async. Client side async is handled automatically by the WCF runtime, even if the implementer did not implement the asynchronous version of the contract.
bobbymcr
@bobbymcr: right, see also http://blogs.msdn.com/mjm/archive/2005/05/04/414793.aspx "sync vs async is a local thing"
Brian