views:

659

answers:

6

Hi all,

I have created a WCF service and client and all works until it come to catching errors. Specifically I am try to catch the EndpointNotFoundException for when the server happens not to be there for whatever reason, I have tried performing a simple try catch block catching 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?

A: 

Removed...........................

paj777
A: 

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... http://stackoverflow.com/questions/36014/why-is-net-exception-not-caught-by-try-catch-block

tbischel
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
A: 

Look at the call stack when the exception gets thrown. There must be a frame between the failure site and yours that catches (and thus handles) the exception.

500 - Internal Server Error
Yes, it does get handled, like I said it's a first chance exception hence the program doesn't break and just carries on. But if the server isn't there I want it to break so I can inform the user and not just have them sitting there wondering why the program isn't doing anything, therefor I would like my code to handle the error.
paj777
What does the call stack look like when the exception is thrown?
500 - Internal Server Error
The call stack is empty at the exact moment the exception is thrown. I have run a SErvice Trace on this and what happens here is first the exception is thrown, then there is a warning that a send message request, then the exception is handled and finally the message is closed.
paj777
A: 

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
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
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
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
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
I don't know, but I think that should have been your original question.
Foole
+2  A: 

Hi, 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.

Jojo Sardez
A: 

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