2

我有一个 wpf 应用程序,它使用 wcf 服务。我希望在服务请求期间捕获任何异常。所以我有这样的东西

 try{  call to service }
  catch(CommunicationObjectFaultedException){}
   catch (EndpointNotFoundException){}

我如何创建一个函数来处理异常,而不是为每个请求执行上述操作?

4

1 回答 1

0

一个 lambda 会完成这项工作,如下所示:

    public static bool TryExecute(Action a) {
        try {
            a();
            return true;
        }
        catch (CommunicationObjectFaultedException) { }
        catch (EndpointNotFoundException) { }
        return false;
    }

并像这样使用:

        bool ok = TryExecute(() => {
            // call to service
            //...
        });
于 2012-05-08T06:33:03.457 回答