Search code examples
c#wcfasynchronousiasyncresult

WCF service aync pattern feedback on failed transmission


I am working in Silverlight 4 and implementing a Polling Duplex service with an asynchronous pattern used to update the clients.


    // interface for messages back to client
    [OperationContract(IsOneWay = true, AsyncPattern=true)]
    IAsyncResult BeginSendMessage(byte[] MessageData, AsyncCallback callback, object State);

    void EndSendMessage(IAsyncResult result);

I make the call back to the client using a RequestState object I defined to keep track of which connected client I sent to.


    AsyncCallback callback = new AsyncCallback(this.MessageSent);
    RequestState state = new RequestState { ConnectionNo = connectionno};
    client.BeginSendMessage(MessageData, callback, state);

I don't see any way to check for an error using the IAsyncResult parameter that is given back in the callback.

So my question is, how can I tell if the message fails to be sent?


Solution

  • I found the answer to my own question thanks to the help in this post Is there a way to get error feedback on asynchronous WCF calls? :)

    When the callback returns, I find the connection in my connection list again using the ConnectionNo I passed through in the state object.

    I then call

    
        try
        {
            //This is the method that is defined in my ServiceContract to complete the AsyncPattern
            client.EndSendMessage(asyncresult);
        }
        catch
        {
            //code here to notify the server that there was an error sending the message
        }