加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

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

发布时间:2020-12-14 02:16:34 所属栏目:Windows 来源:网络整理
导读:我目前正在开发一个 Windows Phone 7应用程序,它调用我也控制的WCF Web服务.该服务提供的操作在给定用户的登录名和密码时返回当前用户的帐户信息: [ServiceContract]public interface IWindowsPhoneService{ [OperationContract] [FaultContract(typeof(Aut
我目前正在开发一个 Windows Phone 7应用程序,它调用我也控制的WCF Web服务.该服务提供的操作在给定用户的登录名和密码时返回当前用户的帐户信息:

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

当然,总是存在身份验证失败的可能性,我想将这些信息传达给WP7应用程序.在这种情况下我可以简单地返回null,但我想传达验证失败的原因(即登录未知,密码错误,帐户被阻止,……).

这是我对上述操作的实现(出于测试目的,它只是抛出异常):

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

现在,如果我在我的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;
    }
}

调试器在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;
}

开始更新1

按F5时,异常气泡到:

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

然后到:

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

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

END UPDATE 1

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

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

>重新添加服务参考 – >没变
>从头开始重新创建一个非常简单的WCF服务 – >同样的问题
>在没有调试器的情况下启动应用程序,以防止应用程序进入调试器 – >好吧,它不会破坏,但也没有抓住异常,应用程序只是退出
>告诉VS 2010不要破坏FaultExceptions(Debug> Options) – >没有任何影响
>在try {…} catch(FaultException){}中包裹我的app中的每一行,甚至catch(Exception) – >从未打电话过

开始更新2

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

>理想情况下,达到GetAccountInfoCompleted(…)并能够通过GetAccountInfoCompletedEventArgs.Error属性检索异常,或者
>能够通过try / catch子句捕获异常

结束更新2

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

解决方法

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读