Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 抛出多种FaultException的最佳方法是什么?_C#_.net_Wcf_Web Services_Soa - Fatal编程技术网

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

C# 抛出多种FaultException的最佳方法是什么?,c#,.net,wcf,web-services,soa,C#,.net,Wcf,Web Services,Soa,我想抛出多种类型的错误异常:validationFault、businessFault、internallServerErrorFault。区分许多不同故障的最佳实践是什么 validationFault-验证后的错误将数据输入到方法 businessFault-任何业务/域除外-无权限,登录名不免费等。 InternallServerError-任何未处理的异常 每个故障都将设置为错误代码 情景1 一种类型是FaultException。BaseException中是带有validationE

我想抛出多种类型的错误异常:validationFault、businessFault、internallServerErrorFault。区分许多不同故障的最佳实践是什么

validationFault-验证后的错误将数据输入到方法 businessFault-任何业务/域除外-无权限,登录名不免费等。 InternallServerError-任何未处理的异常

每个故障都将设置为错误代码

情景1 一种类型是FaultException。BaseException中是带有validationException列表的属性,消息的属性。客户端捕获此FaultException,然后解析错误代码并从正确的属性中提供数据

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
}
试试看
{
}
捕获(FaultException-ex)
{
//在此位置将显示所有故障异常类型。来自错误代码的客户端必须具有
//专用类型的故障验证、业务异常。BaseException将
//be已为验证或业务逻辑异常正确设置数据。
}
捕获(FaultException-ex)
{
//内部服务器错误
}
情景2 单独的故障:ValidationFoult、BusinnesFault、internalServerFault到不同的故障 错误例外 错误例外

ValidationFault-将包含验证错误的数据-带键的字典-属性名称,值-此属性的错误 BusinessFault-将包含消息属性

客户端将捕获此故障。故障异常中的任何故障都将是内部服务器错误

try
{
}
catch (FaultException<ValidationFoult> ex)
{
}
catch (FaultException<BusinessFault> ex)
{
}
catch (FaultException ex)
{
// internal server error
}
试试看
{
}
捕获(FaultException-ex)
{
}
捕获(FaultException-ex)
{
}
捕获(FaultException-ex)
{
//内部服务器错误
}

这个问题的另一个解决方案是什么?有什么建议吗

不需要单独的故障实体,只需创建一个公共实体(可能称为“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;
    }
}

我已经编辑了你的标题。请参阅“”,其中的共识是“不,他们不应该”。验证消息列表如何?。我想注意所有无效的属性。当然,您可以为相同的属性添加集合类型的属性。