服务器端有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/