本文介绍了捕获特定的WCF FaultException异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个单层应用程序可以通过区分异常:

A single tier application can distinguish exceptions by:

Exception ex;

if (ex is System.DirectoryServices.AccountManagement.PasswordException)
    ...

其中前仅仅是一个通用的异常。

where ex is just a generic exception.

当你移动到WCF为多层,你失去这一切,你需要使用的FaultException机制。

When you move to WCF for multi-tier, you lose all this and you need to use the FaultException mechanism.

问题是,我无法找到任何方式做以上。

The problem is that I can't find any way to do the above.

在我的客户我想抓住的FaultException类型,然后将它们即区分是这样的:

In my client I want to catch the FaultException types and then distinguish between them i.e. something like:

catch (FaultException ex)
{
    if FaultException is (PasswordExceptionFault)
      ...
    etc
}

有没有办法做到这一点?

Is there a way to do this?

否则,我必须有很多catch结构 - 每种类型的FaultException的

Otherwise I have to have many catch constructs - one for each type of FaultException.

推荐答案

在使用WCF服务,你必须使用FaulException因为它是处理错误(异常也不能序列)的本地肥皂的方法。

When using WCF service, you have to use FaulException because it's the native Soap approach for handling errors (Exception is also not serializable).

在。典型的场景,你首先要加类型FaultContract每个操作合约

In the typical scenario you will first to add typed FaultContract on each operation contract.

// NOTE: This is the std wcf template
[ServiceContract]
public interface IService1
{
    [FaultContract(typeof(int))]
    [FaultContract(typeof(string))]
    [FaultContract(typeof(DateTime))]
    [OperationContract]
    string GetData(int value);
}

您服务将发送类型的FaultException在您决定客户端的所有故障情况需要故障信息。

You service will send typed faultexception for all fault cases in which you decide the client requires fault information.

您的客户端能赶上表示在操作合同中规定的自定义SOAP故障的FaultException类型。

Your client can catch the FaultException type that represents the custom SOAP fault specified in the operation contract.

 ServiceReference1.Service1Client proxy = new ServiceReference1.Service1Client();

    try
    {
        Console.WriteLine("Returned: {0}", proxy.GetData(-5));
    }
    catch (FaultException<int> faultOfInt)
    {
        //TODO
        proxy.Abort();
    }
    catch (FaultException<string> faultOfString)
    {
        //TODO
        proxy.Abort();
    }
    catch (FaultException<DateTime> faultOfDateTime)
    {
        //TODO
        proxy.Abort();
    }
    catch (FaultException faultEx)
    {
        Console.WriteLine("An unknown exception was received. "
          + faultEx.Message
          + faultEx.StackTrace
        );
        proxy.Abort();
    }
    catch (Exception e)
    {
        //generic method
        Type exceptionType = e.GetType();
        if (exceptionType.IsGenericType && exceptionType.GetGenericTypeDefinition() == typeof(FaultException<>))
        {
            PropertyInfo prop = exceptionType.GetProperty("Detail");
            object propValue = prop.GetValue(e, null);
            Console.WriteLine("Detail: {0}", propValue);
        }
        else
        {
            Console.WriteLine("{0}: {1}", exceptionType, e.Message);
        }
    }



最后,因为FaultException异常继承异常,可以仍然使用反射来获取如下所示内部故障类型和细节。

At the end, because FaultException inherits Exception, you can still use reflection to get inner fault type and detail as shown here.

另外还要注意从WCF客户端上的沟通方法的常见预期的例外情况包括TimeoutException异常,的CommunicationException和任何派生一流的CommunicationException(如FaultException异常)的。这些表明,可以通过中止WCF客户端和报告通信故障安全处理通信过程中出现问题。

Also note that common expected exceptions from communication methods on a WCF client include TimeoutException, CommunicationException, and any derived class of CommunicationException (like FaultException). These indicate a problem during communication that can be safely handled by aborting the WCF client and reporting a communication failure.

这篇关于捕获特定的WCF FaultException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 15:31