我已经创建了一个WCF服务和客户端,它一直工作到捕获错误为止。具体地说,我正在尝试捕捉服务器由于某种原因而恰好不在那里时的EndpointNotFoundException。我已经尝试了一个简单的try/catch块来捕获特定的错误和它所派生的通信异常,并且我尝试只捕获异常。这些方法都不能成功捕获异常,但是我得到了
在System.ServiceModel.dll中出现'System.ServiceModel.EndpointNotFoundException‘类型的第一次机会异常
当客户端尝试打开服务时,将在“输出”窗口中。知道我做错了什么吗?
发布于 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块抛出异常。
尝试编写类似这样的代码:
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
}然后你就能捕捉到正确的异常了。
发布于 2010-04-20 13:53:39
有关此可能的解决方案的详细信息,请查看。代码显示了generate proxy的用法,但在ChannelFactory和其他平台上也是有效的。
典型的here-be-dragons模式
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
}
} 或者,正如您的问题中未使用using语句所表达的,
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();
}发布于 2010-03-17 07:12:47
这可能是调试器的报告问题,而不是实际捕获异常。这篇文章给出了一些解决这个问题的技巧,如果是这样的话…Why is .NET exception not caught by try/catch block?
https://stackoverflow.com/questions/2458631
复制相似问题