views:

39

answers:

3

In my SL4 app, I have a call to a service, svc.SubmitAsync(). Since this is an async call, once the method is hit, my code goes on to the next line. This is fine as long as the user enters the correct user name and password. If they don't, EndSubmit() throws an exception. EndSubmit() is in References.cs, part of the auto-generated Silverlight code.

I tried wrapping svc.SubmitAsync() in a try-catch but this is an async call and the try-catch block completes before the exception is even thrown.

How do I catch this error?

Thanks!

Update 1

public void SubmitTweetAsync(TestSilverlightApp.svc.Tweet tweet, object userState) {
            if ((this.onBeginSubmitTweetDelegate == null)) {
                this.onBeginSubmitTweetDelegate = new BeginOperationDelegate(this.OnBeginSubmitTweet);
            }
            if ((this.onEndSubmitTweetDelegate == null)) {
                this.onEndSubmitTweetDelegate = new EndOperationDelegate(this.OnEndSubmitTweet);
            }
            if ((this.onSubmitTweetCompletedDelegate == null)) {
                this.onSubmitTweetCompletedDelegate = new System.Threading.SendOrPostCallback(this.OnSubmitTweetCompleted);
            }
            base.InvokeAsync(this.onBeginSubmitTweetDelegate, new object[] {
                        tweet}, this.onEndSubmitTweetDelegate, this.onSubmitTweetCompletedDelegate, userState);
        }

Update 2 - This is a WCF service.

A: 

It would normally be the matching completed event that has the try/catch for any exceptions.

As is it an async operation, it does not know if it succeeded or failed until it completes but it will complete in either case.

Enough already
A: 

The object passed to you Completed event handler will need to hold the error message.

For example

public void OnSubmitCompleted(TweetOperation op) { if(op.ErrorMessage != null) //handle the error

}

jeffo
A: 

You have to check the Error field in the parameter you receive in the handler.

private void Client_SubmitCompleted(object sender, SubmitCompletedEventArgs e)
{
    if (e.Cancelled)
    {
        //...
    }
    else if (e.Error != null)
    {
        // the service operation threw an exception
        throw e.Error;
    }
    else
    {
        //...
    }
Francesco De Vittori