我有一个定义 FaultContract 的 WCF 服务:
[OperationContract]
[FaultContract(typeof(InvalidOperationException))]
[FaultContract(typeof(NotSupportedException))]
[FaultContract(typeof(Exception))]
GetSysIdsResponse GetSysIds(string id); 
WCF 服务捕获异常情况(空指针异常)并抛出我的 FaultException:
try
        {
             ....
        }            }
        catch (InvalidOperationException ex)
        {
            // The specified DirectoryEntry is not a container
            throw new FaultException<InvalidOperationException>(ex);
        }
        catch (NotSupportedException ex)
        {
            // Search is not supported by the provider that is being used
            throw new FaultException<NotSupportedException>(ex);
        }
        catch (Exception ex)
        {
            throw new FaultException<Exception>(ex);
        }
它抛出最后一个。问题是它永远不会到达客户端。首先,“故障契约作为服务元数据的一部分发布”。添加服务参考后,我没有在客户端元数据中看到它。这是客户端代码。它永远不会碰到 catch 块 catch (FaultException e) 它只是说用户没有捕获到 FaultException。它确实捕获了 CommunicationException。我不知道我做错了什么?
try
                {
                    response = client.GetSysIds("sgentil");
                }
                catch (FaultException<Exception> e)
                {
                    Console.WriteLine("FaultException<Exception>: " + e.Detail);
                    client.Abort();
                    return;
                }
                catch (FaultException e)
                {
                    Console.WriteLine("FaultException: " + e.Message);
                    client.Abort();
                    return;
                }
                catch (CommunicationException ex)
                {
                    Console.WriteLine("CommunicationsException");
                    client.Abort();
                    return;
                }
** 我尝试了第一个答案的方法并定义了两个例外:
[DataContract]
    public class SystemFault
    {
        [DataMember]
        public string SystemOperation { get; set; }
        [DataMember]
        public string SystemReason { get; set; }
        [DataMember]
        public string SystemMessage { get; set; }
    }
    [DataContract]
    public class DatabaseFault
    {
        [DataMember]
        public string DbOperation { get; set; }
        [DataMember]
        public string DbReason { get; set; }
        [DataMember]
        public string DbMessage { get; set; }
    }
然后我应用了它:
[FaultContract(typeof(SystemFault))]
        GetSysIdsResponse GetSysIds(string id);
然后扔了:
 catch (Exception ex)
            {
                SystemFault sf = new SystemFault
                    {
                        SystemOperation = "GetSystemIds",
                        SystemMessage = "Exception while obtaining AD user properties",
                        SystemReason = ex.Message
                    };
                throw new FaultException<SystemFault>(sf);
            }
客户端现在可以看到 SystemFault 类型并具有该元数据:
 catch(FaultException<SystemFault> sf)
                {
                   Console.WriteLine("SystemFault {0}: {1}\n{2}",
                       sf.Detail.SystemOperation, sf.Detail.SystemMessage,
                       sf.Detail.SystemReason);
                }
然而,执行仍然在该行的服务器中停止:
throw new FaultException<SystemFault>(sf);
它说:用户代码未处理 FaultException'1
怎么办?