【问题标题】:Handling errors within a proxy - WCF处理代理中的错误 - WCF
【发布时间】: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


    【解决方案1】:

    您可以尝试将 try/catch 逻辑封装在处理程序类中,如下所示:

    public static class Example
    {
        public static void ExecuteExample()
        {
            var serviceClient = new ServiceClient();
    
            var documents = ProxyErrorHandler.Execute(serviceClient, serviceClient.GetDocuments);
        }
    }
    
    public static class ProxyErrorHandler
    {
        public static void Execute(ServiceClient serviceClient, Action actionToExecute)
        {
            Execute(serviceClient, () =>
            {
                actionToExecute();
    
                return true;
            });
        }
    
        public static T Execute<T>(ServiceClient serviceClient, Func<T> functionToExecute)
        {
            try
            {
                return functionToExecute();
            }
            catch (Exception exception)
            {
                ShowException(serviceClient, exception);
    
                return default;
            }
        }
    
        public static Task ExecuteAsync(ServiceClient serviceClient, Func<Task> actionToExecute)
        {
            return ExecuteAsync(serviceClient, async () =>
            {
                await actionToExecute();
    
                return true;
            });
        }
    
        public static async Task<T> ExecuteAsync<T>(ServiceClient serviceClient, Func<Task<T>> functionToExecute)
        {
            try
            {
                return await functionToExecute();
            }
            catch (Exception exception)
            {
                ShowException(serviceClient, exception);
    
                return default;
            }
        }
    
        private static void ShowException(ServiceClient serviceClient, Exception exception)
        {
            string title;
            var message = exception.Message;
    
            switch (exception)
            {
                case TimeoutException:
                    title = @"Timeout error";
                    break;
                case FaultException<ServiceErrorDetails> faultException:
                    title = @"Service error";
                    message = faultException.Detail.Message;
                    break;
                case CommunicationException:
                    title = @"Communication error";
                    break;
                default:
                    ExceptionDispatchInfo.Throw(exception);
                    
                    // Unreachable
                    throw new Exception();
            }
    
            MessageBox.Show(message, title, MessageBoxButtons.OK, MessageBoxIcon.Error);
    
            serviceClient.Abort();
        }
    }
    

    【讨论】:

    • 太棒了,谢谢!我没有足够的声誉来支持你:-)
    • @stackMeUp 哈哈,没问题。您可以将其标记为已接受的答案吗?我不确定你是否可以。我很高兴能提供帮助。
    猜你喜欢
    • 2011-09-09
    • 1970-01-01
    • 2011-02-04
    • 1970-01-01
    • 1970-01-01
    • 2011-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多