Can I retrieve variables stored in session on the server?

Hey

I’m saving some info in session on login and some other occasions that I need to read and use on the server. Reading them on the client is no problem, but is there a way to read them on the server?

What I’ve figured out so far is I can do it using ISession session = Global.TheSessionManager.GetSession(sessionID);
but what if I don’t know the sessionID? Is there a way to get existing session between server and client who made the call?

Hope you understand what I’m asking…

Thanks

Hi,

server can process several client’s call at the same time, what will mean “existing” session for you? One session exists during executing service’s method, inside such method you can use Session property of data module. Outside of this you have several session in session manager. Maybe you need last accessed session? You can find it using TROSession.LastAccessed property.

I’ve kind of put this on ice for now since I didn’t find a good solution, but let me try to explain the problem…

We’re receiving files over ftp, and watching the ftp-folder. First in the ftp-transfer we’re receiving an .xml-file with transferinfo.
The client has already logged on and saved some info in the session, and I would like to retrieve this info for saving things properly in the DB.

Here’s some of the code I’ve tested, hopefully it’ll show what I’m trying to achieve:

private ISession GetCurrentSession(TransferInfo transferInfo)
{
foreach (var sessionID in Global.TheSessionManager.GetAllSessions())
{
var tempSession = Global.TheSessionManager.GetSession(sessionID);
string loginName = tempSession[“Login”].ToString();

            if (loginName == transferInfo.User.UserName)
            {
                return tempSession;
            }
        }
        return null;
    }

private void AddXmlTransferReport(DBHandler handler, TransferInfo transferInfo)
{
//ISession currentSession = GetCurrentSession(transferInfo);

        //handler.AddTransferReport(DateTime.Now, double.Parse(currentSession["Latitude"].ToString()), double.Parse(currentSession["Longitude"].ToString()), transferInfo.User.UserName, @"XML Received, File added to DB", @"XML", transferInfo.FileName);
        
    }

Hi,

  1. in folder appears some xml file.
  2. you extract username from it.
  3. in session manager you find session for this username if it found save info to db.

for such scenario your “GetCurrentSession” looks fine.

Yeah, that’s what I thought too, but there was some kind of problem where the session didn’t always exist. Tbh I don’t quite remember exactly what it was now.

I’ll get back to work on this in a little while though, at least I know I’m on the right track :wink:

I’ll let you know what I find if I remember.

Thanks

Actually it might be some problem with Global.TheSessionManager…

I don’t have any logic for adding sessions to it, and I pulled the code directly from one of your examples. Do I have to do anything on my end for making sure the sessions get added?

I seem to have a problem where the session doesn’t always “appear on the server” untill the second call from the client is made to it…

I have this code on my testclient:

private void TestSessionService_Click(object sender, EventArgs e)
{
VizWolfOuterServer.ISessionService myService;
myService = VizWolfOuterServer.CoSessionService.Create(message, clientChannel);

        if (myService != null)
        {
            MessageBox.Show(myService.GetSessionID() + "\n" + myService.GetSessionManagerName() + "\n" + myService.GetSessionValue("Login") + "\n"
                            + myService.GetSessionDevice().Name + "\n" + myService.GetSessionEmployee().Name + "\n" + myService.GetSessionValue("Latitude")
                            + "\n" + myService.GetSessionValue("Longitude"));
        }
    }

This always returns sessioninfo, even if it’s the first thing I do after opening the client (it also catches OnLoginNeeded and logs in).

On the server though, using the following code, the session usually doesn’t show up untill after i’ve made another service-call from the client. Here’s the code:

private void GetAllSessions_Click(object sender, EventArgs e)
{

        string output = "";

        foreach (var sessionID in Global.TheSessionManager.GetAllSessions())
        {
            output += sessionID.ToString() + "\n";
            ISession session = Global.TheSessionManager.GetSession(sessionID);

            var sessionNames = session.Names;

            for (int i = 0; i < sessionNames.Count; i++)
            {
                output += sessionNames[i] + "\n";
                output += session[sessionNames[i]] + "\n\n";
            }
        }

        MessageBox.Show(output);
    }

I think there’s something with the connection with Global.TheSessionManager that I’ve misunderstood

I’ve ended up keeping track of the sessions myself using a global ConcurrentDictionary on the server

This looks like duplication, all session always should are in session manager.

Yeah, that’s what I thought too… Do you see anything wrong with the code I posted above or should I have been able to retrieve all sessions like that?

As I said, using that code, the session usually doesn’t show up untill after i’ve made two service-calls from the client…

Hello.

The code you have provided looks well and should catch all sessions defined on the server side. Please note, that if you need to get current client session you can catch it in DataService _Impl file using this.Session and this.SessionID objects. New session is created after setting any value for Session object, for example:

LoginService_Impl.cs file

            Boolean lIsLoginSuccessful = (userId == password);

            if (lIsLoginSuccessful)
            {
                // This is an example of setting user-specific session information
                this.Session["UserID"] = userId;
                . . .
            }

Thanks.

Hey again

I don’t have a problem accessing the session variables in the services, but I have some classes outside of the services that needs to access the session variables.

From there I’m trying to get current sessions from the memorysessionmanager. As I wrote above I’m trying to find a session that has Session[“Login”] similar to a username i’m getting from an .xml

The problem with this is that, even though the clients catch OnLoginNeeded and logs in, saves variables to session and all, the session doesn’t appear on the memorysessionmanagers list untill the clients have made a second call to a method in a service.

I have since tried this code in a SessionService:

public virtual Guid[] GetAllSessionIDs()
{
Guid[] result;
result = new Guid[SessionManager.GetAllSessions().Count];
SessionManager.GetAllSessions().CopyTo(result, 0);

        return result;
    }

And I’ve tried these two versions on the server (not in a service):

private void GetAllSessions_Click(object sender, EventArgs e)
{

        string output = "";

        var sessionService = CoSessionService.Create(message, new IpSuperHttpClientChannel() { TargetUrl = "superhttp://10.78.20.15:80/bin" } );
       
            foreach (var sessionID in sessionService.GetAllSessionIDs())
            {
                output += sessionID.ToString() + "\n";
             }
        MessageBox.Show(output);
    }

private void GetAllSessions_Click(object sender, EventArgs e)
{

        string output = "";

        using (var sessionService = new SessionService())
        {
            foreach (var sessionID in sessionService.GetAllSessionIDs())
            {
                output += sessionID.ToString() + "\n";
                if (sessionID != null)
                {
                    var session = sessionService.GetSession(sessionID);

                    var sessionNames = session.Names;

                    for (int i = 0; i < sessionNames.Count; i++)
                    {
                        output += sessionNames[i] + "\n";
                        output += session[sessionNames[i]] + "\n\n";
                    }
                
                    output += session.SessionEmployee.Name;
                    output += session.SessionDevice.GUID;
                }
            }
        }
        MessageBox.Show(output);
    }

The first one gives “session could not be found” (the targeturl points back at the server’s own ip), and the second one just gives a blank list (i’m assuming because i’m starting a new SessionService, with new empty sessionmanager and all…)

By the way, how can i format my code like yours?

Hello.

I have tried to reproduce the problem but couldn’t. All works as expected on my testcase. Can you check my testcase and correct it to reproduce the situation?

Also you can format your code using

 tags.

Thanks in advance.

Hey again

Sorry about the late reply, I’ve missed your reply here… I have fixed my problem in this case though.

What I was doing, was that I had a MemorySessionManager on my mainForm.
I also had a MemorySessionManager in a Global.cs class.

The global one was the one I was checking against earlier.

Turns out what was happening, was the MemorySessionManager (from now on MSM) in the mainForm kicked in at once, but on the second servercall the MSM in Global took over. The mainForm MSM then lost the info.

So when I was listing sessions in the Global MSM, new ones didn’t show up on the first call.

I have since removed the Global MSM, and everything now seems to work as I want it to.

Thanks