Session Types sample (.NET)

Overview

This sample demonstrates the essence of sessions and the usage of various session managers:

Prerequisites

Some parts of this sample require the following:

  • Olympia Server to be running on the same computer as the sample server application
  • MS SQL database server available on the same computer as the sample server application
  • MS SQL server should have the Northwind sample database installed and a custom table created in this database:
CREATE TABLE [dbo].[Sessions] (
    [SessionID] [char] (38) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL PRIMARY KEY,
    [Created] [datetime] NULL , [LastAccessed] [datetime] NULL ,
    [Data] [IMAGE] NULL )

Getting Started

  • Build all projects.
  • Launch the server.
  • Activate the desired session manager option.
  • Make sure the required servers are up and running.
  • Launch the client.
  • Perform the login with any UserID; the Password should be equal to the login name.
  • See the Session ID displayed after login.
  • Check the session manager by clicking the Get button next to the Manager Name field.
  • Input Name and Value, then click Set Value. The value will be stored in the session.
  • Erase the content of the Value field, then click Get Value. The value will be read from the session and displayed back to the field.

Examine the Code

  • Session storage is not accessible from the client directly, so we use a couple of methods of the service to get and set values:(SessionService_Impl.cs file):
public virtual string GetSessionValue(string Name) 
{
  return (string)Session[Name];
}

public virtual void SetSessionValue(string Name, string Value) 
{
  Session[Name] = Value;
}
  • Examine the simple login service implementation (LoginService_Impl.cs file). Accessing the Session property creates the session if it is not created yet, calling the DestroySession property destroys it. According to the protected services design guidelines the working service requires the session to be created when this service is called (if the RequireSession property is set to true).
public virtual bool Login(string UserID, string Password) 
{
  if (UserID == Password)
  {
    Session["UserID"] = UserID;
    return true;
  }
  else
  {
    DestroySession();
    return false;
  }
}

public virtual void Logout(string SessionID) 
{
  DestroySession();
}
  • The most sophisticated part is the custom session manager in CustomSessionManager.cs.
// Must subclass the RemObjects.SDK.Server.SessionManager to create a custom session manager
public class CustomSessionManager : RemObjects.SDK.Server.SessionManager, IDisposable
{
    // You can see SQL queries perform various manipulations with sessions data in the database
    const string SESSION_SELECT = "SELECT * FROM Sessions WHERE [SessionID] = @SessionID";
    // ...

    // Adjust this string if necessary
    private string connectionString = "Integrated Security=SSPI;Initial Catalog=Northwind;Data Source=localhost;Workstation ID=localhost";

    //......................

    public CustomSessionManager()
    {
        // The constructor is dedicated to create all necessary objects to talk to the DBMS
        //......................
    }

    // These methods are overridden to implement the actual behaviour we need, i.e. storing the data in the MS SQL database
    
    override protected ISession CreateNewSession(Guid aSessionID)
    {
        // ...
    }

    override public void ReleaseSession(ISession aSession)
    {
        // ...
    }

    override protected int SessionCount
    {
        // ...
    }

    override public void ExpireSessions()
    {
        // ...
    }

    override public void DestroySession(Guid aSessionID)
    {
        // ...
    }

    override protected ISession LocateExistingSession(Guid aSessionID)
    {
        // ...
    }

    //......................
}

Please note: This sample's custom session manager needs to create a session table in one of your databases. By default, the sample looks for the Sessions table in MSSQL's Northwind Database, but you can easily change to connect to another database by changing the connection string in CustomSessionManager.cs.