简体   繁体   English

抛出多种FaultException的最佳方法是什么?

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

I want to throw many kind of fault exception: validationFault, businessFault, internallServerErrorFault. 我想抛出多种故障异常:validationFault,businessFault,internallServerErrorFault。 What is best practice separate many different fault 什么是最佳实践,将许多不同的错误分开

validationFault - errors after validation input data to method businessFault - any business/domain excepion - no permission, login name is not free etc. internallSerrverError - any unhandled excepion validationFault-验证输入数据到方法businessFault后的错误-任何业务/域例外-没有权限,登录名不免费等。internallSerrverError-任何未处理的例外

Each fault will be set errorCode 每个故障都会被设置为errorCode

Scenario 1 One type FaultException. 方案1一种类型FaultException。 In BaseException is property with list of validationException, property for Message. BaseException中的属性是带有validationException的列表,Message的属性是列表。 Client catch this FaultException then parse errorCode and give data from properly properties 客户端捕获此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
}

Scenario 2 Separate faults: ValidationFoult, BusinnesFault, internalServerFault to different faults FaultException FaultException 方案2的单独错误:ValidationFoult,BusinnesFault,internalServerFault到不同的错误FaultException FaultException

ValidationFault - will be contain data for validation error - dictionary with key - property name, value - error for this property BusinessFault - will be contain message property ValidationFault-将包含验证错误的数据-带键的字典-属性名称,值-此属性的错误BusinessFault-将包含消息属性

Client will be catch separate this fault. 客户将被单独捕获此故障。 Any fault in Fault exception will be internal server error 故障异常中的任何故障都将是服务器内部错误

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

What are another solution for this problem ? 这个问题的另一种解决方案是什么? Any sugestions ? 有任何建议吗?

Instead of separate Fault entities, just create one common entity (maybe called 'CommonFault') and share the error details through properties such as ErrorCode, ErrorMessage, StackTrace (if the service is internal). 代替创建单独的Fault实体,只需创建一个公共实体(可能称为“ CommonFault”)并通过诸如ErrorCode,ErrorMessage,StackTrace之类的属性(如果服务是内部的)共享错误详细信息即可。 Something like the below. 如下所示。

[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.

相关问题 处理和引发FaultException的最佳实践 - Best practice to handle and throw FaultException 抛出什么样的异常? - What kind of exception to throw? 抛出异常的最佳方法 - The best way to throw an exception 考虑到 C# 或 Java 中的这种设计,哪种方式最好同时遵守 DRY 和限制意外功能? - What way would be best to adhere to both DRY and limit unintended functionality given this kind of design in C# or Java? 制作表格行的多个版本((或历史记录))的最佳方法是什么 - What is the best way to make many versions ((Or history)) of Table's rows 在多个对象之间共享对象实例的最佳方法是什么? - What's the best way to share an instance of an object across many objects? 处理跨多个应用程序的电子邮件分发的最佳方法是什么? - What is the best way to handle email distribution across many applications? 与许多用户一起上传大文件的最佳方式是什么? - What is the best way to upload large files with many users? 我怎么知道一个类会抛出什么样的异常? - How can i know what kind of exception a class will throw? 与具有多对多父子关系的表数据绑定ASP.NET TreeView的最佳方法是什么 - What is the Best way to databind an ASP.NET TreeView for table with many to many parent child relationships
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM