【问题标题】:wsHttpBinding, catch FaultException from custom UserNamePasswordValidatorwsHttpBinding,从自定义 UserNamePasswordValidator 捕获 FaultException
【发布时间】:2015-02-17 11:17:05
【问题描述】:

我有一个带有 wsHttpBinding 的 WCF 服务。一切正常,但我在捕获客户端错误时遇到了问题,从我的自定义身份验证器发送。
我使用来自 msdn 的自定义身份验证器代码:
https://msdn.microsoft.com/en-us/library/aa702565(v=vs.110).aspx

// This throws an informative fault to the client.
    throw new FaultException("Unknown Username or Incorrect Password");

这条评论说我们向客户端抛出了一个信息错误,但我无法在客户端捕捉到它:

bool isReachable = false;
                try {
                    isReachable = client.agentIsReachable();
                }
                catch(FaultException faultException){
                    MessageBox.Show(faultException.Message);
                }

在调试时,我可以看到抛出了错误,但我的客户端捕获代码不起作用。我的通讯通道故障,但没有故障异常。然后我抓到一个 .NET ex,说我正在尝试使用有故障的代理。
当我从我的任何服务方法中抛出错误时,一切都很好。我可以在我的客户端上捕获它们。

是否真的可以捕获从 Authenticator 发送的错误。当身份验证失败时,向客户端传递信息性消息的最佳方式是什么?

【问题讨论】:

    标签: wcf wshttpbinding faultexception


    【解决方案1】:

    客户端,您必须捕获的异常不是FaultException,而是MessageSecurityException (using System.ServiceModel.Security)。

    然后您可以使用您捕获的MessagSecurityExceptionInnerException 属性检索您的FaultException。在你的情况下,你最终会得到类似这样的东西:

    catch (MessageSecurityException e)
    {
        FaultException fault = (FaultException) e.InnerException;
        MessageBox.Show(faultException.Message);
    }
    

    我希望它会有所帮助。

    【讨论】:

    • 在某种程度上,这种方法是可以的,但是当从服务器端抛出 FaultException 然后它不会在 SecurityException 中捕获这种类型时它不起作用。
    • 试试这个可能对你有帮助:msdn.microsoft.com/en-us/library/…
    猜你喜欢
    • 1970-01-01
    • 2017-01-15
    • 2013-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-04
    • 1970-01-01
    相关资源
    最近更新 更多