繁体   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