Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.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# 如何在WCF的FaultContract中包含实际异常或自定义异常_C#_Wcf_Faultexception - Fatal编程技术网

C# 如何在WCF的FaultContract中包含实际异常或自定义异常

C# 如何在WCF的FaultContract中包含实际异常或自定义异常,c#,wcf,faultexception,C#,Wcf,Faultexception,我想在WCF的FaultContract中包含一个用户定义的异常。 在我的WCF应用程序中,我想在FaultContract中封装Exception instance/UserDefine Exception实例。 请在下面找到我的UserDefine异常 public class UserExceptions : Exception { public string customMessage { get; set; } public string Result { get;

我想在WCF的FaultContract中包含一个用户定义的异常。 在我的WCF应用程序中,我想在FaultContract中封装Exception instance/UserDefine Exception实例。 请在下面找到我的UserDefine异常

public class UserExceptions : Exception
{
    public string customMessage { get; set; }

    public string Result { get; set; }

    public UserExceptions(Exception ex):base(ex.Message,ex.InnerException)
    {

    }
}

public class RecordNotFoundException : UserExceptions
{
    public RecordNotFoundException(Exception ex): base(ex)
    {

    }
}

public class StoredProcNotFoundException : UserExceptions
{
    public string innerExp { get; set; }

    public StoredProcNotFoundException(Exception ex,string innerExp)
        : base(ex)
    {
        this.innerExp = innerExp;
    }
}

[DataContract]
public class ExceptionFault
{ 
    [DataMember]
    public UserExceptions Exception { get; set; }

    public ExceptionFault(UserExceptions ex)
    {
        this.Exception = ex;
    }
}
我在服务中抛出异常,如下所示

try
        {
            //Some Code
            //Coding Section 
                    throw new RecordNotFoundException(new Exception("Record Not Found"));
            //Coding Section
        }
        catch (RecordNotFoundException rex)
        {
            ExceptionFault ef = new ExceptionFault(rex);
            throw new FaultException<ExceptionFault>(ef,new FaultReason(rex.Message));
        }
        catch (Exception ex)
        {
            throw new FaultException<ExceptionFault>(new ExceptionFault((UserExceptions)ex),new FaultReason(ex.Message));
        }
试试看
{
//一些代码
//编码部分
抛出新的RecordNotFoundException(新异常(“未找到记录”);
//编码部分
}
捕获(RecordNotFoundException rex)
{
异常故障ef=新异常故障(rex);
抛出新的FaultException(ef,newfaultreason(rex.Message));
}
捕获(例外情况除外)
{
抛出新的FaultException(新的ExceptionFault((UserExceptions)ex),新的FaultReason(ex.Message));
}

尝试块捕获CustomException(RecordNotFoundException),但它无法将该异常发送到客户端。

您需要将
FaultContract
属性添加到
OperationContract
方法中,以便SOAP客户端知道预期的异常类型

[OperationContract]
[FaultContract(typeof(MathFault))]
int Divide(int n1, int n2);
您的catch块需要捕获
FaultException

如果希望在DataContract中包含UserExceptions的实现,则可能需要使用KnownType属性,以便SOAP客户端了解以下类型:

[DataContract]
[KnownType(typeof(RecordNotFoundException))]
[KnownType(typeof(StoredProcNotFoundException))]
public class ExceptionFault
{ 
    [DataMember]
    public UserExceptions Exception { get; set; }

    public ExceptionFault(UserExceptions ex)
    {
        this.Exception = ex;
    }
}

感谢Glen的快速响应,但我希望在我的FaultContract中包含原始异常(在您的示例中为MathFault),以便客户能够获得实际异常和其他自定义数据,如操作和问题。简言之,我想序列化实际的异常,并通过wire将其发送给客户端。@Sourabh啊,我明白了。使用SOAP服务,您只能通过DataContract传输客户机知道的自定义类型。添加KnownType属性可能对此有所帮助。为什么我不能继承Exception类并将其标记为[DataContract]?它说扩展类不能序列化。@Sourabh如果您将[Serializable]属性而不是[DataContract]添加到异常中,它将修复此问题。serializable属性不是从基异常类继承的。您还需要为反序列化创建构造函数UserExceptions(SerializationInfo,StreamingContext-context):base(info,context)
[DataContract]
public class MathFault
{    
    private string operation;
    private string problemType;

    [DataMember]
    public string Operation
    {
        get { return operation; }
        set { operation = value; }
    }

    [DataMember]        
    public string ProblemType
    {
        get { return problemType; }
        set { problemType = value; }
    }
}
[DataContract]
[KnownType(typeof(RecordNotFoundException))]
[KnownType(typeof(StoredProcNotFoundException))]
public class ExceptionFault
{ 
    [DataMember]
    public UserExceptions Exception { get; set; }

    public ExceptionFault(UserExceptions ex)
    {
        this.Exception = ex;
    }
}