Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF/C# Unable to catch EndpointNotFoundException

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?

like image 532
paj777 Avatar asked Mar 16 '10 22:03

paj777


2 Answers

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.

like image 82
Jojo Sardez Avatar answered Oct 21 '22 05:10

Jojo Sardez


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();
}
like image 45
Sky Sanders Avatar answered Oct 21 '22 04:10

Sky Sanders