WCF / C#无法捕获EndpointNotFoundException

我已经创建了一个WCF服务和客户端,它一直有效,直到捕获错误。 具体来说,我试图捕获EndpointNotFoundException以便在服务器因任何原因发生不存在时。 我已经尝试了一个简单的try / catch块来捕获特定的错误和它派生的通信exception,并且我尝试捕获Exception。 这些都没有成功捕获exception,但我确实得到了

System.ServiceModel.dll中出现’System.ServiceModel.EndpointNotFoundException’类型的第一次机会exception

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

我能够复制你的问题并感兴趣(因为我需要相同的)。 我甚至研究了一种处理\抓住第一次机会exception的方法,但遗憾的是,对于.net framework 3.5及以下版本,它是不可能的(对于托管代码)。

在我的情况下,无论何时在服务上出错或每次访问羽绒服务时,我总是得到System.ServiceModel.CommunicationObjectFaultedException 。 事实certificate,c#的using语句是因为在场景后面,即使已经遇到exception(即不直接跳转到catch语句), using语句也会关闭服务客户端实例。

using尝试关闭服务客户端实例时,原始exceptionSystem.ServiceModel.EndpointNotFoundException将被新exceptionSystem.ServiceModel.CommunicationObjectFaultedException替换。

我所做的解决方案是不使用using语句,这样每当在try块中遇到exception时,它会立即将exception抛出到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 } 

然后你就能捕捉到正确的例外。

有关此可能解决方案的详细信息,请查看此post 。 该代码显示了使用生成代理,但在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(); } 

这可能是调试器的报告问题,而不是实际捕获exception。 这篇文章给出了解决它的一些技巧,如果是这样的话…… 为什么.NETexception没有被try / catch块捕获?

什么是第一次机会例外?

第一次机会exception消息通常并不意味着代码中存在问题。 对于优雅处理exception的应用程序/组件,第一次机会exception消息让开发人员知道遇到并处理了exception情况。

CompletedMethod中放置一个try catch块。

一个例子:

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