【问题标题】:FaultException handling in client?客户端中的 FaultException 处理?
【发布时间】:2013-05-07 08:23:10
【问题描述】:

我正在尝试在 WCF 服务中实现异常处理。 我希望将此异常抛出给客户端,以便对其进行记录和处理。

我不知道我写的代码是对还是错。 这只会让我“用户代码未处理故障异常” 我应该如何解决以便我可以在客户端处理我的异常?

WCF 服务中的代码:

try
{

}
catch (FaultException fex)
{
    throw fex;
}
catch (Exception ex)
{
    throw ex;
} 

客户端代码:

try
{

}
catch (FaultException fex)
{
    Logger.AddExceptionToDb(fex);
}
catch (Exception ex)
{
    Logger.AddExceptionToDb(ex);
}

【问题讨论】:

    标签: wcf exception exception-handling faultexception


    【解决方案1】:

    您应该申请[FaultContract(YourFaultClass)] 将抛出错误异常的方法的属性。

       [OperationContract]
       [FaultContract(typeof(YourFaultClass))]
       void Foo(int par1);
    

    如何从服务中扔掉它:

    throw new FaultException<YourFaultClass>(new YourFaultClass());
    

    如需更多帮助,请查看:http://msdn.microsoft.com/en-us/library/cc949036.aspx

    【讨论】:

    • FaultException 在客户端如何处理异常?有样品吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-11
    相关资源
    最近更新 更多