WP7:无法在对WCF服务的异步调用中捕获FaultException

Chr*_*sch 5 wcf asynchronous faultexception windows-phone-7

我目前正在开发一个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 PhoneApp.RemoteService.WsAccountInfo Result {
  get {
    base.RaiseExceptionIfNecessary();   // <-- here
    return ((PhoneApp.RemoteService.WsAccountInfo)(this.results[0]));
  }
}
Run Code Online (Sandbox Code Playgroud)

然后到:

private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
{
    if (System.Diagnostics.Debugger.IsAttached)
    {
        // An unhandled exception has occurred; break into the debugger
        System.Diagnostics.Debugger.Break();
    }
}
Run Code Online (Sandbox Code Playgroud)

之后,应用终止(使用或不使用调试器).

END UPDATE 1

现在,我想在我的代码中捕获异常,但我从未有机会,因为我的Completed处理程序永远不会到达.

根据本网站上的类似问题,我已经尝试过以下方法:

  • 重新添加服务引用 - >无更改
  • 从头开始重新创建一个非常简单的WCF服务 - >同样的问题
  • 在没有调试器的情况下启动应用程序以防止应用程序进入调试器 - >好吧,它不会中断,但是也没有捕获到异常,应用程序只是退出
  • 告诉VS 2010不要破坏FaultExceptions(Debug> Options) - >没有任何影响
  • 在try {...} catch(FaultException){}中包裹我的应用程序中的每一行,甚至是catch(Exception) - >从不调用.

开始更新2

我实际想要实现的是以下之一:

  • 理想情况下,到达GetAccountInfoCompleted(...)并能够通过GetAccountInfoCompletedEventArgs.Error属性检索异常,或者

  • 能够通过try/catch子句捕获异常

结束更新2

如果有任何建议可以帮助我解决这个问题,我将不胜感激.

Ste*_*ter 0

该框架似乎读取您的 WsAccountInfo.Result 属性。这会在客户端重新引发异常。但您应该是第一个阅读此属性的人。

我不知道您的 AuthenticationFault 类,它是否有 DataContractAttribute 并且它是已知类型,如http://msdn.microsoft.com/en-us/library/system.servicemodel.faultcontractattribute.aspx中的示例 ?