I currently have the following code for my hub class:
public class MatchMaker : Hub
{
private List<SoloUser> soloUsers = new List<SoloUser>();
//Client Requests
public void findNewPartner(string Country, string State)
{
SoloUser soloUser = soloUsers.FirstOrDefault(s => (s.Country== Country) && (s.State == State));
if (soloUsers.Count > 0)
Clients.partnerRequestResult(soloUsers.FirstOrDefault());
else
{
soloUser = new SoloUser {
Major = Major,
School = School,
SessionId = SessionId,
Token = token
};
soloUsers.Add(soloUser);
}
}
}
The problem is whenever I call this method on the client side, adding a SoloUser object to my soloUsers collection does not save, so when another user calls this function again, the linq statement(soloUsers.FirstOrDefault(s => (s.School == School) && (s.Major == Major))) never finds a SoloUser. How can I make it so my soloUsers collection will save the instance and the objects added to it?
SignalR creates a new hub instance for every call you make (which is something very lightweight, don’t worry about that), therefore your soloUsers list is not persisted between calls.
Make the list static to solve this. Beware that this will need some sort of locking when accessing the list or use one of the concurrent collections to make it thread safe.