views:

30

answers:

1

I'm having some difficulty trapping an exception during my testing. I actually disconnect the service so that the endpoint is not available, and I am attempting to modify my application to handle that possibility.

The problem is that no matter where I put try/catch blocks, I can't seem to catch this thing before it gets to unhandled.

I have tried wrapping both my creation code in try/catch,

this.TopicServiceClient = new KeepTalkingServiceReference.TopicServiceClient();
            this.TopicServiceClient.GetAllTopicsCompleted += new EventHandler<KeepTalkingServiceReference.GetAllTopicsCompletedEventArgs>(TopicServiceClient_GetAllTopicsCompleted);
             this.TopicServiceClient.GetAllTopicsAsync();

as well as the delegate that gets called when the service call is complete.

public void TopicServiceClient_GetAllTopicsCompleted(object sender, KeepTalkingServiceReference.GetAllTopicsCompletedEventArgs e)
        {
            try
            {
...

No dice. Any thoughts?

+2  A: 

I would recommend you use the IAsyncResult. When you generate WCF proxy on the client and get the asynchronous calls, you should get a TopicServiceClient.BeginGetAllTopics() method. This method returns an IAsyncResult object. It also takes an AsyncCallback delegate to call when it completes. When it completes, you call EndGetAllTopics() supplying the IASyncResult (passed to EndGetAllTopics().

You put a try/catch around your call to BeginGetAllTopics() and that should catch the exception you are after. If an exception occurs remotely, i.e., you do in fact connect, but the service throws an exception, that is handled at the point where you call the EndGetAllTopics().

Here is a really simple example (clearly not production) to demonstrate what I am saying. This was written in WCF 4.0.

namespace WcfClient
{
class Program
{
    static IAsyncResult ar;
    static Service1Client client;
    static void Main(string[] args)
    {
        client = new Service1Client();
        try
        {
            ar = client.BeginGetData(2, new AsyncCallback(myCallback), null);
            ar.AsyncWaitHandle.WaitOne();
            ar = client.BeginGetDataUsingDataContract(null, new AsyncCallback(myCallbackContract), null);
            ar.AsyncWaitHandle.WaitOne();
        }
        catch (Exception ex1)
        {
            Console.WriteLine("{0}", ex1.Message);
        }
        Console.ReadLine();
    }
    static void myCallback(IAsyncResult arDone)
    {
        Console.WriteLine("{0}", client.EndGetData(arDone));
    }
    static void myCallbackContract(IAsyncResult arDone)
    {
        try
        {
            Console.WriteLine("{0}", client.EndGetDataUsingDataContract(arDone).ToString());
        }
        catch (Exception ex)
        {
            Console.WriteLine("{0}", ex.Message);
        }
    }
}
}

For the server side exception to propagate back to the client, you need to set the following in the server web config...

<serviceDebug includeExceptionDetailInFaults="true"/>
Les