【发布时间】:2021-05-14 06:54:21
【问题描述】:
可以在here 找到有关如何处理客户端错误的好建议。
复制到这里方便查阅:
MyServiceClient myServiceClient = new MyServiceClient();
try
{
documents = myServiceClient.GetDocuments();
// More code that isn't useful including here ...
myServiceClient.Close();
}
catch (TimeoutException exception)
{
MessageBox.Show(exception.Message, "Timeout error", MessageBoxButtons.OK, MessageBoxIcon.Error);
myServiceClient.Abort();
}
catch (FaultException<ServiceErrorDetails> error)
{
MessageBox.Show(error.Detail.Message, "Service error", MessageBoxButtons.OK, MessageBoxIcon.Error);
myServiceClient.Abort();
}
catch (CommunicationException exception)
{
MessageBox.Show(exception.Message, "Communication error", MessageBoxButtons.OK, MessageBoxIcon.Error);
myServiceClient.Abort();
}
现在我遇到的这个解决方案的问题是我的代理包含许多方法。易于理解,我不想在我的所有方法调用周围添加这个巨大的 try/catch 语句。
相反,我认为从 MyServiceClient() 类中添加错误处理可能是个好主意。
但问题是如何做到这一点,而不用这个 try/catch 语句再次污染这里的所有方法?
您将如何处理?
【问题讨论】:
标签: c# wcf error-handling service proxy