我目前正在开发一个Windows Phone 7应用程序,它调用我也控制的WCF Web服务.该服务提供的操作在给定用户的登录名和密码时返回当前用户的帐户信息:
[ServiceContract]
public interface IWindowsPhoneService
{
[OperationContract]
[FaultContract(typeof(AuthenticationFault))]
WsAccountInfo GetAccountInfo(string iamLogin, string password);
}
Run Code Online (Sandbox Code Playgroud)
当然,总是存在身份验证失败的可能性,我想将这些信息传达给WP7应用程序.在这种情况下我可以简单地返回null,但我想传达身份验证失败的原因(即登录未知,密码错误,帐户被阻止,......).
这是我对上述操作的实现(出于测试目的,它只是抛出异常):
public WsAccountInfo GetAccountInfo(string iamLogin, string password)
{
AuthenticationFault fault = new AuthenticationFault();
throw new FaultException<AuthenticationFault>(fault);
}
Run Code Online (Sandbox Code Playgroud)
现在,如果我在我的WP7应用程序中调用此操作,如下所示:
Global.Proxy.GetAccountInfoCompleted += new EventHandler<RemoteService.GetAccountInfoCompletedEventArgs>(Proxy_GetAccountInfoCompleted);
Global.Proxy.GetAccountInfoAsync(txbLogin.Text, txbPassword.Password);
void Proxy_GetAccountInfoCompleted(object sender, RemoteService.GetAccountInfoCompletedEventArgs e)
{
if (e.Error != null)
{
MessageBox.Show(e.Error.Message);
return;
}
}
Run Code Online (Sandbox Code Playgroud)
调试器在Reference.cs中断,说FaultException'1未处理,这里:
public PhoneApp.RemoteService.WsAccountInfo EndGetAccountInfo(System.IAsyncResult result) {
object[] _args = new object[0];
PhoneApp.RemoteService.WsAccountInfo _result = ((PhoneApp.RemoteService.WsAccountInfo)(base.EndInvoke("GetAccountInfo", _args, result)));
return _result;
}
Run Code Online (Sandbox Code Playgroud)
开始更新1
按F5时,异常气泡到:
public …Run Code Online (Sandbox Code Playgroud)