2013-02-14 82 views
0

我有一個WCF Web服務中的函數,每次發生事件條目時都負責通過SignalR發送通知,並檢查與Evento條目相關的羣集是否有三個或更多如果是,則發送另一個通知。SignalR WebRequest正在中止

功能如下:

public static void SendEvento(string dispositivoId, Eventos evento) 
    { 
     Connection conn = new Connection("http://localhost:65097/Index"); 
     Model1Container context = new Model1Container(); 
     try 
     { 
      conn.Start().Wait(); 
      context.Database.Connection.Open(); 
      Dispositivos dispositivo = DispositivosDao.GetDispositivo(dispositivoId); 
      Pin pin = new Pin(dispositivo.Latitud, dispositivo.Longitud, evento.Fecha, evento.IntensidadMax, dispositivo.UniqueId, dispositivo.Alias); 
      List<Pin> lista = new List<Pin>(); 
      lista.Add(pin); 
      var json = new JavaScriptSerializer().Serialize(lista); 
      //send the notification 
      conn.Send(json).Wait(); 
      Clusters cluster = ClustersDao.GetCluster(dispositivo.ClustersClusterId); 
      List<Dispositivos> dispositivos = cluster.Dispositivos.ToList(); 
      var cont = 0; 
      List<Eventos> listaEventosCluster = new List<Eventos>(); 
      for (var i = 0; cont < 3 && i < dispositivos.Count(); i++) 
      { 
       listaEventosCluster.AddRange(dispositivos.ElementAt(i).Eventos.ToList<Eventos>()); 
      } 
      if (listaEventosCluster.Count() > 0) 
      { 
       listaEventosCluster.OrderByDescending(e => e.Fecha); 
       DateTime endHour = evento.Fecha; 
       DateTime startHour = endHour.AddHours(-1); 
       var inHour = listaEventosCluster.Where(o => o.Fecha >= startHour && o.Fecha <= endHour); 
       int count = inHour.Count(); 
       if (count >= 2) 
       { 
        //send another notification if there're 3 or more entries in the last hour 
        json = new JavaScriptSerializer().Serialize(new { Alerta = "Sismo" }); 
        conn.Send(json).Wait(); 
       } 
      } 
     } 
     catch (Exception ex) 
     { 
      Debug.WriteLine("Error " + ex.Message + " \n Inner Exception " + ex.InnerException + " \n Stack Trace " + ex.StackTrace); 
     } 
     finally 
     { 
      conn.Stop(); 
      conn.Disconnect(); 
     } 
    } 

我沒有問題,發送通知,但會拋出異常eveytime有在最後一小時3名或更多的條目。

這是輸出我得到異常:

iisexpress.exe Error: 0 : SignalR exception thrown by Task: System.AggregateException: One or more errors occurred. ---> System.Net.WebException: The request was aborted: The request was canceled. 
    at System.Net.ConnectStream.EndRead(IAsyncResult asyncResult) 
    at Microsoft.AspNet.SignalR.Infrastructure.StreamExtensions.<>c__DisplayClass4.<ReadAsync>b__1(IAsyncResult ar) 
    at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization) 
    --- End of inner exception stack trace --- 
---> (Inner Exception #0) System.Net.WebException: The request was aborted: The request was canceled. 
    at System.Net.ConnectStream.EndRead(IAsyncResult asyncResult) 
    at Microsoft.AspNet.SignalR.Infrastructure.StreamExtensions.<>c__DisplayClass4.<ReadAsync>b__1(IAsyncResult ar) 
    at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)<--- 

iisexpress.exe Error: 0 : SignalR exception thrown by Task: System.AggregateException: One or more errors occurred. ---> System.AggregateException: One or more errors occurred. ---> System.Net.WebException: The request was aborted: The request was canceled. 
    at System.Net.ConnectStream.EndRead(IAsyncResult asyncResult) 
    at Microsoft.AspNet.SignalR.Infrastructure.StreamExtensions.<>c__DisplayClass4.<ReadAsync>b__1(IAsyncResult ar) 
    at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization) 
    --- End of inner exception stack trace --- 
    --- End of inner exception stack trace --- 
---> (Inner Exception #0) System.AggregateException: One or more errors occurred. ---> System.Net.WebException: The request was aborted: The request was canceled. 
    at System.Net.ConnectStream.EndRead(IAsyncResult asyncResult) 
    at Microsoft.AspNet.SignalR.Infrastructure.StreamExtensions.<>c__DisplayClass4.<ReadAsync>b__1(IAsyncResult ar) 
    at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization) 
    --- End of inner exception stack trace --- 
---> (Inner Exception #0) System.Net.WebException: The request was aborted: The request was canceled. 
    at System.Net.ConnectStream.EndRead(IAsyncResult asyncResult) 
    at Microsoft.AspNet.SignalR.Infrastructure.StreamExtensions.<>c__DisplayClass4.<ReadAsync>b__1(IAsyncResult ar) 
    at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)<--- 
<--- 

我不明白這是爲什麼異常被拋出,即使我的服務性能沒有被篡改,我擔心它可能造成問題。我怎樣才能擺脫這個問題?

回答

1

調用Connection.Stop()將中止任何掛起的HTTP請求,這是設計。還有其他方法可以乾淨地殺死正在運行的http請求。

+0

所以我應該只打一個電話給Connection.Disconnect()?由於我的方法是靜態的,完成連接的最佳方法是什麼? – 2013-02-14 16:54:29

+0

只需呼叫停止並讓它中止連接。 – davidfowl 2013-02-15 07:06:39

+0

好的,謝謝你的回答。 – 2013-02-15 17:32:03