简体   繁体   English

无法使用 WSDualHttpBinding 在 WCF 客户端中捕获 FaultException

[英]Not able to catch FaultException in WCF client with WSDualHttpBinding

I have a WCF server with a custom UserNamePasswordValidator.我有一个带有自定义 UserNamePasswordValidator 的 WCF 服务器。

public class CustomUserNameValidator : UserNamePasswordValidator
{
    private readonly string m_userName;
    private readonly string m_password;

    /// <summary>
    ///  CustomUserNameValidator
    /// </summary>
    /// <param name="userName"></param>
    /// <param name="password"></param>
    public CustomUserNameValidator(string userName, string password)
    {
        m_userName = userName;
        m_password = password;
    }

    /// <summary>
    /// Validate
    /// </summary>
    /// <param name="userName"></param>
    /// <param name="password"></param>
    public override void Validate(string userName, string password)
    {
        if (!(userName == m_userName && password == m_password))
        {
            throw new FaultException("Authentication failed!");
        }
    }
}

On the client side, I call a function with wrong credentials added to the proxy在客户端,我调用了 function,但将错误的凭据添加到代理

try
{
    return service.GetServiceDescription();
}
catch (FaultException)
{
}
catch (TimeoutException)
{
}

In the debugger of the server I can see, that the FaultException is thrown.在我可以看到的服务器调试器中,抛出了 FaultException。 But the client will never receive this exception.但是客户端永远不会收到这个异常。 It will end up in a TimeoutException on client side.它最终会在客户端出现 TimeoutException。

If I use the right credentials, the function an server side will be called and ervery thing works well, so the wcf is working correct.如果我使用正确的凭据,将调用服务器端的 function 并且一切正常,因此 wcf 工作正常。

But what do I have to do, that the FaultException will be thrown to the client, so that I can realize, that the credentials are wrong on client side?但是我该怎么办,FaultException 将被抛出给客户端,这样我才能意识到客户端的凭据是错误的?

UPDATE: It works, when I use the WSHttpBinding, the I am able to catch a MessageSecurityException.更新:它有效,当我使用 WSHttpBinding 时,我能够捕获 MessageSecurityException。 But when I am using WSDualHttpBinding and a dual channel, then I am running into a time out.但是当我使用 WSDualHttpBinding 和双通道时,我遇到了超时。

You should not (and as far as I know can not) have exceptions crossing over from service to client.你不应该(据我所知不能)有从服务到客户端的异常。 The correct way to handle things like this, is to return a value that indicates if the user was valid.处理此类事情的正确方法是返回一个指示用户是否有效的值。 You may also want to include some information about the cause of a user not being valid.您可能还希望包含一些有关导致用户无效的原因的信息。

Another thing about your approach.关于你的方法的另一件事。 It's considered bad practice to use Exceptions for dealing with actual business logice (like validating a user).使用异常处理实际业务逻辑(如验证用户)被认为是不好的做法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM