gpt4 book ai didi

wcf - WP7 : Unable to catch FaultException in asynchronous calls to WCF service

转载 作者:行者123 更新时间:2023-12-03 17:57:09 24 4
gpt4 key购买 nike

我目前正在开发一个 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();
}
}

之后,应用程序终止(有或没有调试器)。

结束更新 1

现在,我很想在我的代码中捕获异常,但我从来没有机会,因为我的 Completed 处理程序从未到达。

基于本网站上的类似问题,我已经尝试了以下方法:
  • 重新添加服务引用-->无变化
  • 从头开始重新创建一个非常简单的 WCF 服务 --> 同样的问题
  • 在没有调试器的情况下启动应用程序以防止应用程序闯入调试器 --> 好吧,它没有中断,但也没有捕获异常,应用程序只是退出
  • 告诉 VS 2010 不要中断 FaultExceptions(调试 > 选项)--> 没有任何影响
  • 将我的应用程序中的每一行都包装在 try { ... } catch (FaultException) {} 或什至 catch (Exception) --> 从未调用过。

  • 开始更新 2

    我真正想要实现的是以下之一:
  • 理想情况下,到达 GetAccountInfoCompleted(...) 并能够通过 GetAccountInfoCompletedEventArgs.Error 属性或
  • 检索异常
  • 能够通过 try/catch 子句捕获异常

  • 结束更新 2

    我将不胜感激任何可以帮助我解决此问题的建议。

    最佳答案

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

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

    关于wcf - WP7 : Unable to catch FaultException in asynchronous calls to WCF service,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9768263/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com