18

I have created a WCF service and client and it all works until it comes to catching errors. Specifically I am trying to catch the EndpointNotFoundException for when the server happens not to be there for whatever reason. I have tried a simple try/catch block to catch the specific error and the communication exception it derives from, and I've tried catching just Exception. None of these succeed in catching the exception, however I do get

A first chance exception of type 'System.ServiceModel.EndpointNotFoundException' occurred in System.ServiceModel.dll

in the output window when the client tries to open the service. Any ideas as to what I'm doing wrong?

Community
  • 1
  • 1
paj777
  • 255
  • 1
  • 4
  • 12
  • Maybe you're trying to catch it in the wrong place. Have you tried enabling "Break when an exception is Thrown"? (Debug -> Exceptions from the menu in VS) – Igby Largeman Mar 16 '10 at 22:59
  • Yes I have, so I know that I'm catching it in the right place. – paj777 Mar 16 '10 at 23:01
  • Humour us and post the code that's failing. I have a hunch, but there's no sense in writing up an answer without more info. – Aaronaught Mar 20 '10 at 22:11
  • I've actually solved the issue now. I changed the development settings to C# in VS which gives you the option of user handled exception. I'd been working on C++ prior to this and hadn't realised I had different development option in C# until I used a different machine with settings. A bit stupid of me really. – paj777 Mar 22 '10 at 17:24
  • @Fyodor - what he is saying, i think, is that an option to break on all that was not visible with C environment is visible in C# environment. – Sky Sanders Apr 20 '10 at 05:50
  • Have a look here as well http://thorarin.net/blog/post/2010/05/30/Indisposable-WCF-clients.aspx – NoWar Oct 10 '12 at 11:12

5 Answers5

4

I was able to replicate your issue and got interested (since I needed the same). I even researched a way to handle \ catch first chance exceptions but unfortunately it is not possible (for managed code) for .net framework 3.5 and below.

On my case I always get a System.ServiceModel.CommunicationObjectFaultedException whenever something gets wrong on the service or whenever I access a down service. It turns out that c#'s using statement is the cause since behind the scene, the using statement always closes the service client instance even if an exception was already encountered (it doesn't jump to catch statement directly).

What happens is that the original exception System.ServiceModel.EndpointNotFoundException will be replaced by the new exception System.ServiceModel.CommunicationObjectFaultedException whenever the using tries to close the service client instance.

The solution i've made is to not use the using statement so that whenever an exception is encountered inside the try block it will instantly throw the exception to the catch blocks.

Try to code something like:

DashboardService.DashboardServiceClient svc = new Dashboard_WPF_Test.DashboardService.DashboardServiceClient();
try
{
    svc.GetChart(0);
}
catch (System.ServiceModel.EndpointNotFoundException ex)
{
    //handle endpoint not found exception here
}
catch (Exception ex)
{
    //general exception handler
}
finally
{
    if (!svc.State.Equals(System.ServiceModel.CommunicationState.Faulted) && svc.State.Equals(System.ServiceModel.CommunicationState.Opened))
    svc.Close();
}

Instead of:

try
{
    using (DashboardService.DashboardServiceClient svc = new Dashboard_WPF_Test.DashboardService.DashboardServiceClient())
    {
        svc.GetChart(0);
    }
}
catch (System.ServiceModel.EndpointNotFoundException ex)
{
    //handle endpoint not found exception here (I was never able to catch this type of exception using the using statement block)
}
catch (Exception ex)
{
    //general exception handler
}

And you'll be able to catch the right exception then.

Itai Bar-Haim
  • 1,686
  • 16
  • 40
Jojo Sardez
  • 8,400
  • 3
  • 27
  • 38
3

Take a look at this post for details on this possible solution. The code shows use of a generate proxy but is valid on ChannelFactory and others as well.

Typical here-be-dragons pattern

using (WCFServiceClient c = new WCFServiceClient())
{
    try
    {
        c.HelloWorld();
    }
    catch (Exception ex)
    {
        // You don't know it yet but your mellow has just been harshed.

        // If you handle this exception and fall through you will still be cheerfully greeted with 
        // an unhandled CommunicationObjectFaultedException when 'using' tries to .Close() the client.

        // If you throw or re-throw from here you will never see that exception, it is gone forever. 
        // buh bye.
        // All you will get is an unhandled CommunicationObjectFaultedException
    }
} // <-- here is where the CommunicationObjectFaultedException is thrown

Proper pattern:

using (WCFServiceClient client = new WCFServiceClient())
{
    try
    {
        client.ThrowException();

    }
    catch (Exception ex)
    {
        // acknowledge the Faulted state and allow transition to Closed
        client.Abort();

        // handle the exception or rethrow, makes no nevermind to me, my
        // yob is done ;-D
    }
} 

Or, as expressed in your question without a using statement,

WCFServiceClient c = new WCFServiceClient();

try
{
    c.HelloWorld();
}
catch
{
    // acknowledge the Faulted state and allow transition to Closed
    c.Abort();

    // handle or throw
    throw;
}
finally
{
    c.Close();
}
Sky Sanders
  • 36,396
  • 8
  • 69
  • 90
1

This may be a reporting issue for the debugger, rather than not actually catching the exception. this post gives some tips on resolving it, if that is the case... Why is .NET exception not caught by try/catch block?

Community
  • 1
  • 1
tbischel
  • 6,337
  • 11
  • 51
  • 73
  • Had a quick look at that thread but there didn't seem to be anything there to help in this case. This isn't an unhandled error, the program does not crash, the exception gets handled just not by my program. – paj777 Mar 16 '10 at 23:49
0

Place a try catch block in the CompletedMethod.

An Example:

...
geocodeService.ReverseGeocodeCompleted += ReverseGeocodeCompleted(se, ev);
geocodeService.ReverseGeocodeAsync(reverseGeocodeRequest);
}

    private void ReverseGeocodeCompleted(object sender, ReverseGeocodeCompletedEventArgs e)
    {
      try
        {
            // something went wrong ...
            var address = e.Result.Results[0].Address;

        }
        catch (Exception)
        { // Catch Exception
            Debug.WriteLine("NO INTERNET CONNECTION");
        }
Stofkn
  • 1,428
  • 16
  • 24
0

What is a First Chance Exception?

First chance exception messages most often do not mean there is a problem in the code. For applications / components which handle exceptions gracefully, first chance exception messages let the developer know that an exceptional situation was encountered and was handled.

Foole
  • 4,754
  • 1
  • 26
  • 23
  • I am aware of what a first chance exception is. In this case I know that the exception is occuring because the server isn't there and I would like to tell the user of the system that the server isn't there, therefor I would like my code to handle the exception. – paj777 Mar 17 '10 at 11:13
  • I that case, I would use Reflector to track down the code where this exception is caught and see if it triggers any event or sets any state that allows you to detect this condition. – Foole Mar 17 '10 at 12:35
  • What I have decided to is to check the connection is open before making the request to the server: if (client.State == CommunicationState.Opened) { client.ServerRequest(); } If not then I have an event handler to deal with this scenario. – paj777 Mar 17 '10 at 16:39
  • No dice on my solution as it hangs when I try to Open the server if an endpoint isn't found and Refelector didn't lead me anywhere either. Is there anyway of checking an endpoint in WCF? – paj777 Mar 17 '10 at 19:26
  • I don't know, but I think that should have been your original question. – Foole Mar 18 '10 at 00:39