【发布时间】: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