首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WCF/C#无法捕获EndpointNotFoundException

WCF/C#无法捕获EndpointNotFoundException
EN

Stack Overflow用户
提问于 2010-03-17 06:31:18
回答 5查看 19.6K关注 0票数 18

我已经创建了一个WCF服务和客户端,它一直工作到捕获错误为止。具体地说,我正在尝试捕捉服务器由于某种原因而恰好不在那里时的EndpointNotFoundException。我已经尝试了一个简单的try/catch块来捕获特定的错误和它所派生的通信异常,并且我尝试只捕获异常。这些方法都不能成功捕获异常,但是我得到了

在System.ServiceModel.dll中出现'System.ServiceModel.EndpointNotFoundException‘类型的第一次机会异常

当客户端尝试打开服务时,将在“输出”窗口中。知道我做错了什么吗?

EN

回答 5

Stack Overflow用户

发布于 2010-04-20 13:46:28

我能够复制你的问题,并产生了兴趣(因为我需要同样的东西)。我甚至研究了一种处理第一次机会异常的方法,但不幸的是,对于.net Framework3.5及更低版本,这是不可能的(对于托管代码)。

在我的例子中,每当服务出现问题或访问关闭的服务时,我总是得到一个System.ServiceModel.CommunicationObjectFaultedException。事实证明,c#的using语句是原因,因为在幕后,using语句总是关闭服务客户端实例,即使已经遇到异常(它不会直接跳到catch语句)。

每当using尝试关闭服务客户端实例时,原始异常System.ServiceModel.EndpointNotFoundException将被新的异常System.ServiceModel.CommunicationObjectFaultedException替换。

我提出的解决方案是不使用using语句,这样只要在try块中遇到异常,它就会立即向catch块抛出异常。

尝试编写类似这样的代码:

代码语言:javascript
复制
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();
}

而不是:

代码语言:javascript
复制
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
}

然后你就能捕捉到正确的异常了。

票数 4
EN

Stack Overflow用户

发布于 2010-04-20 13:53:39

有关此可能的解决方案的详细信息,请查看。代码显示了generate proxy的用法,但在ChannelFactory和其他平台上也是有效的。

典型的here-be-dragons模式

代码语言:javascript
复制
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

正确的模式:

代码语言:javascript
复制
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
    }
} 

或者,正如您的问题中未使用using语句所表达的,

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2010-03-17 07:12:47

这可能是调试器的报告问题,而不是实际捕获异常。这篇文章给出了一些解决这个问题的技巧,如果是这样的话…Why is .NET exception not caught by try/catch block?

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2458631

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档