简体   繁体   中英

How do you save/restore SignalR Hub, Connections, State on App Shutdown/Startup

I looked at this excellent SO post but it doesn't give me enough detail. I want to store everything about a SignalR Hub - it's connections, state, users on application shutdown and retrieve on app startup. We have an issue with clients seem to lose their connections if we request data via SignalR and then we do a Web Publish to the Web site.

Here is what I am doing now. In OnDisconnected:

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

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

            }

These are two tables in the 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; }
    }

and

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

        public string UserName { get; set; }

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

Should I do something else on Startup/Shutdown in Global.asax? I also have a thread-safe Dictionary to route our data through:

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

Should that get serialized too? Thanks!

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

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

        }

The Database tables are:

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

I probably when do something else on Startup/Shutdown in Global.asax?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM