简体   繁体   English

如何在Wcf服务上引发异常并在客户端上捕获它?

[英]How to throw Exceptions on Wcf Service and catch it on client?

Service Demo Code: 服务演示代码:

  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");
            }
        }  
    }

Client Code Demo: 客户端代码演示:

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 Demo: 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>

When i throw FaultException , the service crashs. 当我抛出FaultException ,服务崩溃。 I would like to be able to catch the FaultException on client and keep the service working. 我希望能够在客户端上捕获FaultException并保持服务正常运行。

I hope to have explained my problem well and provided the nescessary code. 我希望能很好地解释我的问题并提供必要的代码。

WCF uses Faults to throw errors across from the server to the client. WCF使用Faults从服务器到客户端抛出错误。 You can use a FaultException to start and then create your own custom faults as needed. 您可以使用FaultException启动,然后根据需要创建自己的自定义错误。

So, in your example, you could simply do the following. 因此,在您的示例中,您可以简单地执行以下操作。

throw new FaultException("Invalid login")

This article provides a whole lot more detail on how to accomplish faults with WCF. 本文提供了有关如何完成WCF故障的更多详细信息。

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

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