.net - 如何在客户端拦截 WCF 错误

标签 .net wcf

服务器端有IErrorHandler等

在客户端上,当您调用 channel 上的方法时会抛出异常。

有没有办法在方法调用过程中抛出异常之前在客户端拦截异常?

最佳答案

不,您不能在异常发生之前拦截它们……或者您真正想要实现的目标是什么?

您需要在客户端做的基本上只是防御性编程:将您的服务调用放入 try...catch... block 并处理您可以(并且想要)的异常句柄:

MyServiceClient client = new MyServiceClient();

try
{
    client.MakeYourCall();
}
catch(EndpointNotFoundException ex)
{
   // tell the user the endpoint doesn't exist
}      
catch(CommunicationException ex)
{
   // catch the WCF base exception to handle all other WCF related issues, if needed
}      
catch(TimeoutException ex)
{
   // tell the user a timeout occured
}      

所有与 WCF 相关的异常都是基础 CommunicationException 的后代类(除了,正如我最近了解到的:QuotaExceededException)——如果你能对它们使用react,捕获更具体的。

然后是系统范围的 TimeoutException这也可能是您想要捕捉的东西(它不是特定于 WCF 的,因此它不会从 CommunicationException 派生)

关于.net - 如何在客户端拦截 WCF 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3012613/

相关文章:

c# - WebApi 文件读取访问被拒绝

wcf - 我需要为 net.tcp 服务设置什么 SPN?

.net - 如何在传输级别压缩来自 WCF .NET 的 HTTP 请求?

c# - Global.asax.cs 文件在哪里?

c# - HttpBasicAuthenticator 身份验证问题

.net - 异步HTTP请求导致跨线程异常

c# - 在 WCF 客户端中查找流对象的长度?

c# - 从 dotnet 使用 Java Web 服务的最佳实践是什么

c# - 使用 WCF PollingDuplex 和 Silverlight 客户端时出现 MaxSessionsPerAddress 问题

c# - ActiveMQ - 没有从 DurableConsumer 收到主题消息