WCF / C#无法捕获EndpointNotFoundException [英] WCF/C# Unable to catch EndpointNotFoundException

查看:107
本文介绍了WCF / C#无法捕获EndpointNotFoundException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个WCF服务和客户端,一直工作,直到遇到错误。具体来说,我试图抓住 EndpointNotFoundException ,因为在任何原因的情况下,服务器不会出现在那里。我已经尝试了一个简单的try / catch块来捕获特定的错误和它从中导致的通信异常,我已经尝试捕捉到异常。没有这些成功捕获异常,但是我得到

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


类型
'System.ServiceModel的第一次机会异常。当客户端尝试打开服务时,输出窗口中发生EndpointNotFoundException'
在System.ServiceModel.dll

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?

推荐答案

我能够复制你的问题并感兴趣(因为我需要一样)。我甚至研究了一种处理\捕获第一次机会异常的方法,但不幸的是,对于.net框架3.5及以下,不可能(对于托管代码)。

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.

在我的案例每当服务发生错误或每次访问down服务时,我总是得到一个 System.ServiceModel.CommunicationObjectFaultedException 。事实证明,使用 语句的c#的 是幕后的原因, 使用 语句总是关闭服务客户端实例,即使已经遇到异常(它不直接跳转到catch语句)。

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).

会发生什么,原来的例外 System.ServiceModel.EndpointNotFoundException 将被替换每当 使用 ,新的异常 System.ServiceModel.CommunicationObjectFaultedException strong>尝试关闭服务客户端实例。

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.

我所做的解决方案是不要使用 使用 语句,以便每当尝试块中遇到异常时,它将立即将异常抛出到catch块。

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.

尝试编写类似的代码:

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

而不是:

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.

这篇关于WCF / C#无法捕获EndpointNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆