简体   繁体   English

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

[英]WP7: Unable to catch FaultException in asynchronous calls to WCF service

I am currently developing a Windows Phone 7 App that calls a WCF web service which I also control. 我目前正在开发一个Windows Phone 7应用程序,它调用我也控制的WCF Web服务。 The service offers an operation that returns the current user's account information when given a user's login name and password: 该服务提供的操作在给定用户的登录名和密码时返回当前用户的帐户信息:

[ServiceContract]
public interface IWindowsPhoneService
{
    [OperationContract]
    [FaultContract(typeof(AuthenticationFault))]
    WsAccountInfo GetAccountInfo(string iamLogin, string password);
}

Of course, there is always the possibility of an authentication failure and I want to convey that information to the WP7 app. 当然,总是存在身份验证失败的可能性,我想将这些信息传达给WP7应用程序。 I could simply return null in that case, but I would like to convey the reason why the authentication failed (ie login unknown, wrong password, account blocked, ...). 在这种情况下我可以简单地返回null,但我想传达身份验证失败的原因(即登录未知,密码错误,帐户被阻止,......)。

This is my implementation of the above operation (for testing purposes, all it does is throwing an exception): 这是我对上述操作的实现(出于测试目的,它只是抛出异常):

public WsAccountInfo GetAccountInfo(string iamLogin, string password)
{
    AuthenticationFault fault = new AuthenticationFault();
    throw new FaultException<AuthenticationFault>(fault);
}

Now, if I call this operation in my WP7 app, like this: 现在,如果我在我的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;
    }
}

The debugger breaks in Reference.cs, saying that FaultException'1 was unhandled, here: 调试器在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;
}

BEGIN UPDATE 1 开始更新1

When pressing F5, the exception bubbles to: 按F5时,异常气泡到:

public PhoneApp.RemoteService.WsAccountInfo Result {
  get {
    base.RaiseExceptionIfNecessary();   // <-- here
    return ((PhoneApp.RemoteService.WsAccountInfo)(this.results[0]));
  }
}

and then to: 然后到:

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

After that, the app terminates (with or without the debugger). 之后,应用终止(使用或不使用调试器)。

END UPDATE 1 END UPDATE 1

Now, I would love to catch the exception in my code, but I am never given the chance, since my Completed handler is never reached. 现在,我想在我的代码中捕获异常,但我从未有机会,因为我的Completed处理程序永远不会到达。

Based on similar questions on this site, I have already tried the following: 根据本网站上的类似问题,我已经尝试过以下方法:

  • Re-add the service reference --> no change 重新添加服务引用 - >无更改
  • Re-create a really simple WCF service from scratch --> same problem 从头开始重新创建一个非常简单的WCF服务 - >同样的问题
  • Start the app without the debugger to keep the app from breaking into the debugger --> well, it doesn't break, but the exception is not caught either, the app simply exits 在没有调试器的情况下启动应用程序以防止应用程序进入调试器 - >好吧,它不会中断,但是也没有捕获到异常,应用程序只是退出
  • Tell VS 2010 not to break on FaultExceptions (Debug > Options) --> does not have any effect 告诉VS 2010不要破坏FaultExceptions(Debug> Options) - >没有任何影响
  • wrap every line in my app in try { ... } catch (FaultException) {} or even catch (Exception) --> never called. 在try {...} catch(FaultException){}中包装我的应用程序中的每一行,甚至是catch(Exception) - >从不调用。

BEGIN UPDATE 2 开始更新2

What I actually would like to achieve is one of the following: 我实际想要实现的是以下之一:

  • ideally, reach GetAccountInfoCompleted(...) and be able to retrieve the exception via the GetAccountInfoCompletedEventArgs.Error property, or 理想情况下,到达GetAccountInfoCompleted(...)并能够通过GetAccountInfoCompletedEventArgs.Error属性检索异常,或者

  • be able to catch the exception via a try/catch clause 能够通过try / catch子句捕获异常

END UPDATE 2 结束更新2

I would be grateful for any advice that would help me resolve this issue. 如果有任何建议可以帮助我解决这个问题,我将不胜感激。

The framework seems to read your WsAccountInfo.Result property. 该框架似乎读取了您的WsAccountInfo.Result属性。 This rethrows the exception on client side. 这会在客户端重新抛出异常。 But you should be the first to read this property. 但你应该是第一个阅读这个属性的人。

I don't know your AuthenticationFault class, does it have a DataContractAttribute and is it a known type like the example in http://msdn.microsoft.com/en-us/library/system.servicemodel.faultcontractattribute.aspx ? 我不知道你的AuthenticationFault类,它是否有一个DataContractAttribute,它是一个已知的类型,如http://msdn.microsoft.com/en-us/library/system.servicemodel.faultcontractattribute.aspx中的示例?

I believe I had the same problem. 我相信我遇到了同样的问题。 I resolved it by extending the proxy class and calling the private Begin.../End... methods within the Client object rather than using the public auto-generated methods on the Client object. 我通过扩展代理类并在Client对象中调用私有的Begin ... / End ...方法而不是在Client对象上使用公共自动生成的方法来解决它。

For more details, please see: http://cbailiss.wordpress.com/2014/02/09/wcf-on-windows-phone-unable-to-catch-faultexception/ 有关详细信息,请参阅: http//cbailiss.wordpress.com/2014/02/09/wcf-on-windows-phone-unable-to-catch-faultexception/

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

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