0

Hi in my application I am using SignalR to store the user and signalR connection mapping. I am adding connections and user mapping on OnConnected() event and removing connections on OnDisconnected() event.

OnDisconnected event is getting called when a file is downloaded from application, which removes the user connections. Below is the code for file download

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

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.

Below is the code for SignalR events

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

Does any one have any idea why onDisconnected event is getting called on File download().

4

2 回答 2

1

最后,我找到了问题的解决方案:使用 signalR 客户端 API 在断开连接的事件上附加一个处理程序,并将超时设置为 2 或 3 秒,然后重新连接集线器。如果下载大文件需要时间,此方法也将起作用,因为只有在 SignalR HUB 断开连接时才会触发事件,无论文件下载需要多少时间。

下面是代码

connection.hub.disconnected(function () {
        setTimeout(function () {
            //Connect to hub again
            $.connection.hub.start();
        }, 3000);
    });
于 2017-09-28T11:03:39.777 回答
0

我们通过添加target="_blank"暂时“解决”了这个问题:

<a href="@Url.Action("DownloadFile", "Controller")" target="_blank"></a>

但我们仍在寻找更好的解决方案,因此我们不需要使用 target="_blank"。因此,非常感谢任何其他解决方案。

于 2017-09-28T09:06:25.637 回答