私は現在、私も制御する 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終了
この問題の解決に役立つアドバイスをいただければ幸いです。