【问题标题】:WCF FaultException<T> is not caught in client, instead caught as service faultWCF FaultException<T> 未在客户端中捕获,而是作为服务错误捕获
【发布时间】:2015-11-01 15:40:39
【问题描述】:

我为 FaultException 配置了服务,但在客户端我没有在

中捕获异常
catch (FaultException<MyServiceFault> fe)
{
} 

相反,它总是被困在

catch (FaultException fx) 
{ 
}

我正在使用 selfhost 和 channelfactory。

我的服务:

[FaultContract(typeof(MyServiceFault))]
public string HelloWorld()
{
    int a=5;
    try
    {
        var b = a/0;
    }
    catch(Exception e)
    {
        throw new FaultException<MyServiceFault>(new MyServiceFault(){Message ="Divide by zero"}, "Divide by Zero");
    }
}

我在MyServiceFault 上也有[DataContract] 属性。 我想知道我是否遗漏了任何配置。

【问题讨论】:

  • 这一切对我来说都是正确的;你能包括你得到的故障异常的详细信息吗?

标签: wcf exception faultexception


【解决方案1】:

我在这里回答了类似的问题:Proper way to throw exception over WCF

尝试像这样声明你的操作:

[FaultContractAttribute(
        typeof(MyServiceFault),
        Action = "", 
        Name = "MyServiceFault", 
        Namespace = "YourNamespace")]
public string HelloWorld()

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多