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

服务演示代码:

  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并保持服务正常运行。

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

参考方案

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

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

throw new FaultException("Invalid login")

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

jQuery不起作用 - php

我正在使用带有ajax的jquery。有时,给出错误$未定义。这是我的代码:<script language="javascript" type="text/javascript"> var base_path="<? echo $this->baseUrl().'/…

如何以编程方式扩展Jquery Slider Panel? - c#

我有用于登录的JQuery Silder面板|注册选项。供参考,我已包含此滑块Jquery Login | Register Slider Panel的演示实际的问题是,当位于“文字”控件中的“登录”部分或“注册新帐户”部分中有值时,如何以编程方式扩展此滑块。为了理解这个问题,我把我的设计代码。 <asp:Panel ID="Panel_Lo…

Div单击与单选按钮相同吗? - php

有没有一种方法可以使div上的click事件与表单环境中的单选按钮相同?我只希望下面的div提交值,单选按钮很丑代码输出如下:<input id="radio-2011-06-08" value="2011-06-08" type="radio" name="radio_date&#…

无法解析Json响应 - php

我正在尝试解析一个包含产品类别的JSON文件,然后解析这些类别中的产品,并在div中显示它们。我的问题:虽然我可以获取类别,但我不知道如何索取产品(并将它们显示在类别下)。我的剧本:<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script> …

<T>如何在这里处理String和Integer - java

我不明白T如何使用Integer和String。如此处显示功能中所示,T同时处理整数和字符串。该代码如何工作?class firstBase { <T> void display(T give_num, T give_String) { System.out.println("The given number is = " +…