簡體   English   中英

拋出多種FaultException的最佳方法是什么?

[英]What is best way to throw many kind of FaultException?

我想拋出多種故障異常:validationFault,businessFault,internallServerErrorFault。 什么是最佳實踐,將許多不同的錯誤分開

validationFault-驗證輸入數據到方法businessFault后的錯誤-任何業務/域例外-沒有權限,登錄名不免費等。internallSerrverError-任何未處理的例外

每個故障都會被設置為errorCode

方案1一種類型FaultException。 BaseException中的屬性是帶有validationException的列表,Message的屬性是列表。 客戶端捕獲此FaultException,然后解析errorCode並從適當的屬性提供數據

try
{
}
catch (FaultException<BaseException> ex)
{
 // in this place will be all fault exception type. From error code client must have   
 // dedicated kind of fault - validation, business exception. BaseException will 
 // be has properly set data for validation or business logic exception.
}
catch (FaultException ex)
{
// internal server error
}

方案2的單獨錯誤:ValidationFoult,BusinnesFault,internalServerFault到不同的錯誤FaultException FaultException

ValidationFault-將包含驗證錯誤的數據-帶鍵的字典-屬性名稱,值-此屬性的錯誤BusinessFault-將包含消息屬性

客戶將被單獨捕獲此故障。 故障異常中的任何故障都將是服務器內部錯誤

try
{
}
catch (FaultException<ValidationFoult> ex)
{
}
catch (FaultException<BusinessFault> ex)
{
}
catch (FaultException ex)
{
// internal server error
}

這個問題的另一種解決方案是什么? 有任何建議嗎?

代替創建單獨的Fault實體,只需創建一個公共實體(可能稱為“ CommonFault”)並通過諸如ErrorCode,ErrorMessage,StackTrace之類的屬性(如果服務是內部的)共享錯誤詳細信息即可。 如下所示。

[DataContractAttribute]
public class CommonFault
{
    private string errorcode;
    private string errormessage;
    private string stackTrace;

    [DataMemberAttribute]
    public string ErrorCode
    {
        get { return this.code; }
        set { this.code = value; }
    }

    [DataMemberAttribute]
    public string Message
    {
        get { return this.message; }
        set { this.message = value; }
    }

    [DataMemberAttribute]
    public string StackTrace
    {
        get { return this.stackTrace; }
        set { this.stackTrace = value; }
    }

    public CommonFault()
    {
    }
    public CommonFault(string code, string message, string stackTrace)
    {
        this.Code = code;
        this.Message = message;
        this.StackTrace = stackTrace;
    }
}

暫無
暫無

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

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