5

私は現在、私も制御する WCF Web サービスを呼び出す Windows Phone 7 アプリを開発しています。このサービスは、ユーザーのログイン名とパスワードを指定すると、現在のユーザーのアカウント情報を返す操作を提供します。

[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 サービスをゼロから再作成します --> 同じ問題
  • デバッガーなしでアプリを起動して、アプリがデバッガーに侵入しないようにします-> まあ、壊れませんが、例外もキャッチされず、アプリは単に終了します
  • FaultExceptions ([デバッグ] > [オプション]) で中断しないように VS 2010 に指示します --> 効果はありません
  • アプリのすべての行を try { ... } catch (FaultException) {} または catch (Exception) でラップします --> 呼び出されませんでした。

アップデート 2 を開始

私が実際に達成したいのは、次のいずれかです。

  • 理想的には、GetAccountInfoCompleted(...) に到達し、GetAccountInfoCompletedEventArgs.Error プロパティを介して例外を取得できるようにするか、または

  • try/catch 句を介して例外をキャッチできる

アップデート2終了

この問題の解決に役立つアドバイスをいただければ幸いです。

4

2 に答える 2

0

私は同じ問題を抱えていたと思います。Client オブジェクトで自動生成されたパブリック メソッドを使用するのではなく、プロキシ クラスを拡張し、Client オブジェクト内でプライベート Begin.../End... メソッドを呼び出すことで解決しました。

詳細については、http: //cbailiss.wordpress.com/2014/02/09/wcf-on-windows-phone-unable-to-catch-faultexception/を参照してください。

于 2014-02-09T12:20:46.490 に答える
0

フレームワークは WsAccountInfo.Result プロパティを読み取っているようです。これにより、クライアント側で例外が再スローされます。しかし、このプロパティを最初に読む必要があります。

あなたのAuthenticationFaultクラスはわかりません.DataContractAttributeがあり、 http://msdn.microsoft.com/en-us/library/system.servicemodel.faultcontractattribute.aspxの例のような既知のタイプですか?

于 2012-04-30T16:33:43.140 に答える