簡體   English   中英

WCF錯誤異常未傳播到客戶端

[英]WCF Fault Exception not being propagated to client

我有一個WCF服務,它定義了FaultContract:

[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)。它只是說明Fault未被用戶捕獲。 它確實捕獲了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不由用戶代碼處理

怎么辦?

就我而言,如果沒有用以下屬性標記接口,則響應為空:

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

您可以使用這些屬性標記操作,以使響應顯示故障詳細信息。

問題是您指定的FaultContractXXXException類型的。 我認為這行不通,您必須創建自己的自定義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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM