【问题标题】:How to throw Exceptions on Wcf Service and catch it on client?如何在 Wcf 服务上抛出异常并在客户端上捕获它?
【发布时间】:2017-01-21 18:20:41
【问题描述】:

服务演示代码:

  public class Login : UserNamePasswordValidator
    {
        public override void Validate(string userName, string password)
        {
            if (new ProjectContext().Users.Count(x => x.Username == userName && x.Password == password) == 0)
            {
                throw new FaultException("Invalid login");
            }
        }  
    }

客户端代码演示:

internal bool LoginOnWcf(string address, string password) 
        {
            try
            {
                service.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None;
                service.ClientCredentials.UserName.UserName = address;
                service.ClientCredentials.UserName.Password = password;
                user = service.GetUserById(address);
                return true;
            }
            catch (FaultException ex)
            {
                MessageBox.Show(ex.Message);
                return false;
            }
        }

Web.config 演示:

<behaviors>
      <serviceBehaviors>
        <behavior name="defaultProfile">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
          <serviceCredentials>
            <clientCertificate>
              <authentication certificateValidationMode="None" />
            </clientCertificate>
            <serviceCertificate findValue="App1" storeLocation="CurrentUser"
              storeName="My" x509FindType="FindBySubjectName" />
            <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="App1.App_Code.Authentication.Login, App_Code/Authentication"/>
          </serviceCredentials>
        </behavior>
        <behavior>
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
</behaviors>

当我抛出 FaultException 时,服务崩溃。 我希望能够在客户端捕获FaultException 并保持服务正常运行。

我希望能很好地解释我的问题并提供必要的代码。

【问题讨论】:

    标签: c# wcf xaml exception exception-handling


    【解决方案1】:

    WCF 使用故障从服务器向客户端抛出错误。您可以使用FaultException 启动,然后根据需要创建自己的自定义故障。

    因此,在您的示例中,您可以简单地执行以下操作。

    throw new FaultException("Invalid login")
    

    article 提供了有关如何使用 WCF 完成故障的更多详细信息。

    【讨论】:

    • 你能检查我的问题更新吗?其实我真的无法在客户端上捕捉到Exception
    • 1.您确定是您抛出的错误导致了问题而不是其他异常吗?您可能需要考虑将整个方法包装在 try catch 中并从那里抛出错误。 2.您完全编辑了使我的答案不再有效的问题。您最初的问题表明缺乏理解错误,我的回答解决了这些错误。如果你对故障有新的问题,你真的应该问一个单独的问题。
    • 真的很抱歉。问题是“当此异常类型为用户未处理时中断”选项已激活。你可能想用这个picture 和这个question 来改进你的答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 2013-01-13
    • 2019-09-12
    • 1970-01-01
    相关资源
    最近更新 更多