WCF / C#无法捕获EndpointNotFoundException怎么办?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (26)

我创建了一个WCF服务和客户端,它一直运行,直到捕获错误。具体而言,我试图赶上EndpointNotFoundException服务器发生不出于任何原因时在那里。我尝试了一个简单的try / catch块来捕获特定错误和它从中派生出来的通信异常,并且我尝试捕获Exception。这些都没有成功捕捉异常,但我确实得到了

A first chance exception of type 'System.ServiceModel.EndpointNotFoundException' occurred in System.ServiceModel.dll

在客户端尝试打开服务时的输出窗口中。任何想法,我做错了什么?

提问于
用户回答回答于

看看这个帖子,了解这个可能的解决方案的细节。该代码显示了生成代理的使用,但在ChannelFactory等上也是有效的。

典型的这里模式

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

适当的模式:

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
    }
} 

或者,如你的问题中没有使用陈述所表达的那样,

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();
}
用户回答回答于

我能够复制你的问题,并有兴趣(因为我需要相同的)。我甚至研究过处理\ catch一次机会异常的方法,但不幸的是,对于.net framework 3.5及更低版本,这是不可能的(对于托管代码)。

在我的情况下,我总是会System.ServiceModel.CommunicationObjectFaultedException在服务出现问题或每次访问停机服务时收到。事实证明,using自从幕后开始,c#的声明是原因,using即使已经遇到异常(它不会直接跳转到catch语句),该语句总是关闭服务客户端实例。

会发生什么情况是,只要尝试关闭服务客户端实例,原始异常System.ServiceModel.EndpointNotFoundException就会被新异常所取代。System.ServiceModel.CommunicationObjectFaultedExceptionusing

我所做的解决方案是不使用using语句,以便在try块内遇到异常时立即将异常抛出到catch块。

尝试编写如下代码:

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
}

然后你就可以找到正确的例外。

扫码关注云+社区