2

我有一个定义 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

怎么办?

4

2 回答 2

1

就我而言,如果接口未标记以下属性,则响应为空:

[System.ServiceModel.XmlSerializerFormatAttribute(SupportFaults = true)]
[System.ServiceModel.ServiceKnownTypeAttribute(typeof(Fault))]

您可以使用这些属性标记您的操作,以使响应显示故障详细信息。

于 2017-08-02T13:23:24.617 回答
0

问题是您指定您FaultContract的类型是XXXException. 我认为这行不通,您必须创建FaultContract自己的自定义,例如:

[DataContract]
public class InitializationFault
{
    public InitializationFault(Exception exc, string msg)
    {
        Exception = exc;
        Message = msg;
    }

    [DataMember]
    public Exception Exception { get; set; }

    [DataMember]
    public string Message { get; set; }
}

那么你的ServiceContract变成:

[OperationContract]
[FaultContract(typeof(InitializationFault))]
//..more
GetSysIdsResponse GetSysIds(string id); 

并且您的客户端代码变为:

try
{
    response = client.GetSysIds("sgentil");
}
catch (FaultException<InitializationFault> e)
{
     Console.WriteLine("FaultException<InitializationFault>: " + e.Detail);
    //more
}
于 2013-05-02T15:02:59.337 回答