0

SignalRを使用してサーバーにアクセスするSilverlight5アプリケーションがあります。ほとんどの場合、これは問題なく機能しますが、デバッグで実行すると、ネゴシエーション中にエラーが発生することがあります。これは、ローカルのISSインストールで接続が不足していることが原因である可能性があると考えています。

とにかく、これが発生すると、アプリケーションのUnhandledExceptionハンドラーが呼び出されます。これを変更して、SignalRの初期化に対してローカルな例外を処理できるようにします。ネゴシエーションエラーが中央のエラーハンドラに伝播しないようにします。

次のようなコードがあります。

     var uri = Application.Current.Host.Source.GetComponents(UriComponents.Scheme | UriComponents.HostAndPort, UriFormat.Unescaped);

     _hubConnection = new HubConnection(uri);
     var hub = _hubConnection.CreateProxy("stuffhub");
     hub.On<Stuff>("Stuff", message => DoStuff());

     var transport = new LongPollingTransport();
     _hubConnection.Start(transport);

このコードはほとんどの場合正常に実行されますが、上記のコードが実行されてから数秒後に、次の例外が中央の未処理の例外ハンドラーに渡されることがあります。

Message: Exception(s) occurred : .
[ Exception(s) occurred : .
[ Exception(s) occurred : .
[ Exception(s) occurred : .
[ System.InvalidOperationException: Server negotiation failed.
   vid SignalR.Client.Transports.HttpBasedTransport.<GetNegotiationResponse>b__0(IResponse     response)
   vid SignalR.TaskAsyncHelper.FromMethod[T1,TResult](Func`2 func, T1 arg) ]
 ]
 ]

]

ApplicationUnhandledExceptionEventArgsのStackTraceは次のとおりです。

 at System.Threading.Tasks.Task.Finalize()
4

2 に答える 2

1

どのタスクが例外をスローしているのか正確にはわかりませんが、それが自分で制御しているタスクの場合は、継続を追加するだけです。

task.ContinueWith(t => {
    // Depending on t.Status (cancelled or faulted) you could perform logging
    // or whatever... presumably nothing's watching for the task to complete,
    // otherwise *it* would see the exception...
}, TaskContinuationOptions.NotOnRanToCompletion);

もちろん、.NET 4.5とC#5を使用できる場合は、async/await役立つ場合があります。これはSignalRの機能によって異なります。

于 2012-09-13T22:39:46.907 に答える
1

Jon Skeetが言ったように、Start()から返されたタスクからの例外を処理する必要があります。

var uri = Application.Current.Host.Source.GetComponents(UriComponents.Scheme | UriComponents.HostAndPort, UriFormat.Unescaped);

 _hubConnection = new HubConnection(uri);
 var hub = _hubConnection.CreateProxy("stuffhub");
 hub.On<Stuff>("Stuff", message => DoStuff());

 _hubConnection.Start().ContinueWith(task => 
 {
     if(task.IsFaulted) {
         Debug.WriteLine("Error starting -" + task.Exception.GetBaseException());
     }
 });
于 2012-09-14T03:57:38.700 に答える