title: SignalR onDisconnected(bool stopCalled) getting called on File download in MVC
body: <p>Hi in my application I am using SignalR to store the user and signalR connection mapping. I am adding connections and user mapping on <strong>OnConnected()</strong> event and removing connections on <strong>OnDisconnected()</strong> event.</p>
  
  <p>OnDisconnected event is getting called when a file is downloaded from application, which removes the user connections. Below is the code for file download</p>
  
  <pre><code> public ActionResult DownloadFile(string fileName)
      {
          // adding time stamp to file name
          fileName = fileName.FileNameWithTimeStamp();
  
          //Fetch file bytes from TempData
          byte[] fileContent = (byte[])TempData[Constants.ExportedData];
          return File(fileContent, Constants.ExcelContentType, fileName);
      }
  </code></pre>
  
  <p>OnReconnected or OnConnected events are not get called subsequently, which results is the data loss (user connection mapping stored on OnConnectedEvent). So system does not able to send notification to removed user connection.</p>
  
  <p>Below is the code for SignalR events</p>
  
  <pre><code>public override Task OnDisconnected(bool stopCalled)
      {
          string name = Context.User.Identity.Name;
          //Remove data to cache
          cache.Remove(name.ToLower(),Context.ConnectionId);
  
          return base.OnDisconnected(stopCalled);
      }
  
  
   public override Task OnConnected()
      {
          string name = Context.User.Identity.Name.ToLower();
          if(!string.IsNullOrEmpty(name))
          {               
              cache.Add(name, Context.ConnectionId);
          }
          return base.OnConnected();
      }
  </code></pre>
  
  <p>Does any one have any idea why <strong>onDisconnected</strong> event is getting called on File download().</p>