如何在App Shutdown / Startup上保存/恢复SignalR Hub,Connections,State

时间:2014-03-26 20:29:00

标签: c# asp.net signalr

我看了这个excellent SO post,但它没有给我足够的细节。我想存储有关SignalR Hub的所有内容 - 它是应用程序关闭时的连接,状态,用户以及应用程序启动时的检索。我们遇到一个问题,如果我们通过SignalR请求数据,然后我们对Web站点进行Web发布,则客户似乎失去了联系。

这是我现在正在做的事情。在OnDisconnected中:

var connection = db.SignalRConnections.Find(Context.ConnectionId);
                    connection.Connected = false;
                    db.SaveChanges();

在OnConnected中:

public override Task OnConnected()
        {
            try
            {
                var name = Context.User.Identity.Name;

                using (savitasEntities2 entities = new savitasEntities2())
                {
                    var user = entities.SignalRUsers
                        .Include(u => u.SignalRConnections)
                        .SingleOrDefault(u => u.UserName == name);

                    if (user == null)
                    {
                        user = new SignalRUser
                        {
                            UserName = name,
                            SignalRConnections = new List<SignalRConnection>()
                        };
                        entities.SignalRUsers.Add(user);
                    }

                    user.SignalRConnections.Add(new SignalRConnection
                    {
                        ConnectionID = Context.ConnectionId,
                        UserAgent = Context.Request.Headers["User-Agent"],
                        Connected = true
                    });
                    entities.SaveChanges();
                }

            }

这些是DB中的两个表:

 public partial class SignalRConnection
    {
        public string ConnectionID { get; set; }
        public string UserAgent { get; set; }
        public bool Connected { get; set; }
        public string SignalRUser_UserName { get; set; }

        public virtual SignalRUser SignalRUser { get; set; }
    }

public partial class SignalRUser
    {
        public SignalRUser()
        {
            this.SignalRConnections = new HashSet<SignalRConnection>();
        }

        public string UserName { get; set; }

        public virtual ICollection<SignalRConnection> SignalRConnections { get; set; }
    }
}

我应该在Global.asax的启动/关机上做些什么吗?我还有一个线程安全的字典来路由我们的数据:

public static ConcurrentDictionary<string, DataSet> publicDataDictionary = new ConcurrentDictionary<string, DataSet>();

是否应该序列化?谢谢!

1 个答案:

答案 0 :(得分:0)

var connection = db.SignalRConnections.Find(Context.ConnectionId);
                connection.Connected = false;
                db.SaveChanges();

OnConnected:

public override Task OnConnected()
    {
        try
        {
            var name = Context.User.Identity.Name;

            using (savitasEntities2 entities = new savitasEntities2())
            {
                var user = entities.SignalRUsers
                    .Include(u => u.SignalRConnections)
                    .SingleOrDefault(u => u.UserName == name);

                if (user == null)
                {
                    user = new SignalRUser
                    {
                        UserName = name,
                        SignalRConnections = new List<SignalRConnection>()
                    };
                    entities.SignalRUsers.Add(user);
                }

                user.SignalRConnections.Add(new SignalRConnection
                {
                    ConnectionID = Context.ConnectionId,
                    UserAgent = Context.Request.Headers["User-Agent"],
                    Connected = true
                });
                entities.SaveChanges();
            }

        }

数据库表是:

 public partial class SignalRConnection
    {
        public string ConnectionID { get; set; }
        public string UserAgent { get; set; }
        public bool Connected { get; set; }
        public string SignalRUser_UserName { get; set; }

        public virtual SignalRUser SignalRUser { get; set; }
    }
public partial class SignalRUser
    {
    public SignalRUser()
    {
        this.SignalRConnections = new HashSet<SignalRConnection>();
    }

    public string UserName { get; set; }

    public virtual ICollection<SignalRConnection> SignalRConnections { get; set; }
}
}

我可能在Global.asax的启动/关机时做了其他什么?