WCF/C# Unable to catch EndpointNotFoundException

2019-02-16 05:55发布

问题:

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?

回答1:

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.



回答2:

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();
}


回答3:

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?



回答4:

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.



回答5:

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");
        }