SignalR WebRequest 正在中止

Posted

技术标签:

【中文标题】SignalR WebRequest 正在中止【英文标题】:SignalR WebRequest is being aborted 【发布时间】:2013-02-14 00:30:11 【问题描述】:

我在 WCF Web 服务中有一个函数,负责在每次创建 Evento 条目时通过 SignalR 发送通知,它还会检查与 Evento 条目相关的集群在过去一小时内是否有三个或更多条目,如果是,则发送另一个通知。

函数如下所示:

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();
        
    

我发送通知没有问题,但在过去一小时内有 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 请求。

【讨论】:

所以我应该只调用 Connection.Disconnect() 吗?由于我的方法是静态的,那么完成连接的最佳方法是什么? 只需调用 stop 并让它中止连接。

以上是关于SignalR WebRequest 正在中止的主要内容,如果未能解决你的问题,请参考以下文章

VB.net webrequest.uploadfile 中止错误

(40)C#里使用WebRequest和出错:请求被中止: 未能创建 SSL/TLS 安全通道

(40)C#里使用WebRequest和出错:请求被中止: 未能创建 SSL/TLS 安全通道

Invoke-WebRequest : 请求被中止: 未能创建 SSL/TLS 安全通道。

Powershell Invoke-WebRequest 和字符编码

请求被中止:请求被取消。没有解决方案有效