This project has moved. For the latest updates, please go here.

Add references

You should add references in your ASP.NET web application to:

  1. The Homelidays.Web.SessionService.dll whose location is in the Global Assembly Cache.
    This is how the file was added at Homelidays (at that time NuGet did not exist yet):
    1. Close your Visual Studion Solution
    2. Open with a text editor (like Notepad++ the csproj where you want to add the SessionService.
    3. Identify where the assemlies like System are referenced
    4. Add <Reference Include="Homelidays.Web.SessionService" />
    5. Save and close the file in the text editor
    6. Open the solution in Visual Studio
  2. The log4net.dll
    The assembly could be found in the installation folder.
    If you choose the default installation path and depending if you install x64 or x86 versions, you may found this folder either on:
    1. C:\Program Files\Homelidays\Session Service x64 or
    2. C:\Program Files\Homelidays\Session Service x86.

Configure the Homelidays.Web.SessionService assembly

The assembly needs an appSettings whose key is SessionServiceConfigDirectory to set its configuration folder (i.e. the folder where the  AspSessionServiceConfigAdo.Net.xml is located) so that it can get its connection string.
This location is typically the installation folder but can be any folder where your application has read access.
To do it simply add the following appSettings in the Web.Config of your web application.

<appSettings>
   
<add key="SessionServiceConfigDirectory" value="C:\Program Files\Homelidays\Session Service x64"/>
</appSettings>

/!\ Update the path according to where you installed the Homelidays Session Service /!\ 

Option 1: Integrating the SessionService via code behind (ASP.NET Web Form only)

Any .aspx page should inherit the Homelidays.Web.SessionService.SessionPage class that inherit System.Web.UI.Page.

Homelidays.Web.SessionService.SessionPage expose an AspSession member that allows you to access the ASP session.

Your code behind should looks like :

using  Homelidays.Web.SessionService;

public partial class SessionService_Test : SessionPage
{
   protected void Page_Load(object sender, EventArgs e)
    {
        this.AspSession["DotNetIntValue"] = 12;
        foreach (var item in this.AspSession)
        {
            LContentSess.Text += "Key : " + item.Key + " Value : " + item.Value + "<br/>";
        }

        if (((int)this.AspSession["DotNetIntValue"]) == 12)
        {
            Response.Write("blabla");
        }
    }
}

In this example, the aspx page should contain a label:

<asp:Label ID="LContentSess" runat="server" Text=""></asp:Label>

If the session is needed in an Handler (.ashx) you will need to take inspiration from the Homelidays.Web.SessionService.SessionPage to access the Homelidays Session Service your self (not a big deal).

Option 2: Integrating the SessionService via a module (ASP.NET Web Form and ASP.NET MVC)

The principle is to instanciate the SessionService in the beginRequest event and to store the session to the database in EndRequest event.

namespace MvcApplication1

{

    using System;

    using System.Web;

 

    using Homelidays.Web.SessionService;

 

    /// <summary>

    /// Exemple module that shows how to use the session service in a ASP.NET application

    /// </summary>

    public class MyModule1 : IHttpModule

    {

        /// <summary>

        /// You will need to configure this module in the Web.config file of your

        /// web and register it with IIS before being able to use it. For more information

        /// see the following link: http://go.microsoft.com/?linkid=8101007

        /// </summary>

        #region IHttpModule Members

 

        /// <summary>

        /// Disposes of the resources (other than memory) used by the module.

        /// </summary>

        public void Dispose()

        {

        }

 

        /// <summary>

        /// Initializes the MyModule1 module and prepares it to handle requests.

        /// </summary>

        /// <param name="application">An System.Web.HttpApplication that provides access to the methods, properties,

        /// and events common to all application objects within an ASP.NET application</param>

        public void Init(HttpApplication application)

        {

            application.BeginRequest += this.ApplicationBeginRequest;

            application.EndRequest += this.ApplicationEndRequest;

        }

 

        #endregion

 

        /// <summary>

        /// ASP.NET begin request event.

        /// </summary>

        /// <param name="source">An System.Web.HttpApplication that provides access to the methods, properties,

        /// and events common to all application objects within an ASP.NET application</param>

        /// <param name="e">HttpApplication event argument</param>

        private void ApplicationBeginRequest(object source, EventArgs e)

        {

            var application = (HttpApplication)source;

            HttpContext context = application.Context;

 

            if (this.IsSessionRequest(context))

            {

                var aspSession = new AspSession(context);

                context.Items["HomelidaysAspSession"] = aspSession;

            }

        }

 

        /// <summary>

        /// ASP.NET end request event.

        /// </summary>

        /// <param name="source">An System.Web.HttpApplication that provides access to the methods, properties,

        /// and events common to all application objects within an ASP.NET application</param>

        /// <param name="e">HttpApplication event argument</param>

        private void ApplicationEndRequest(object source, EventArgs e)

        {

            var application = (HttpApplication)source;

            HttpContext context = application.Context;

 

            if (this.IsSessionRequest(context))

            {

                var aspSession = context.Items["HomelidaysAspSession"] as AspSession;

 

                if (aspSession != null)

                {

                    // A l'unload de la page on fait la persistence de la session en base

                    aspSession.PersistSession();

                }

            }

        }

 

        /// <summary>

        /// Retruns true if the request uses the session (ex: .aspx uses the session but you may define that .ashx will never use the session)

        /// </summary>

        /// <param name="context">Current Http context</param>

        /// <returns>True if the Session object should be linked to the current request, else False</returns>

        private bool IsSessionRequest(HttpContext context)

        {

            // TODO - filter here the queries that uses the Homelidays SessionService

            return true;

        }

    }

}

 

Update the web.config to use the module

<system.web>

   …

     <httpModules>

      …

        <!-- For IIS classic mode only -->

      <add name="SessionServiceModule" type="MvcApplication1.MyModule1" />

    </httpModules>
</
system.web>

<system.webServer>

    …

       <!-- For IIS integrated mode only -->

    <modules>

      …

      <add name="SessionServiceModule" type="MvcApplication1.MyModule1" />

    </modules>
</
system.webServer>

Create an abstract class ControllerBase that inherit the Controller class. All your controllers that need the session service will required to inherit from the ControllerBase class.

namespace MvcApplication1.Controllers

{

    using System;

    using System.Web.Mvc;

 

    using Homelidays.Web.SessionService;

 

    /// <summary>

    /// The controller base.

    /// </summary>

    public abstract class ControllerBase : Controller

    {

        /// <summary>

        /// The session state : FOR SESSION SERVICE

        /// </summary>

        private AspSession aspSession;

 

        /// <summary>

        /// Gets the session state : FOR SESSION SERVICE

        /// </summary>

        public AspSession AspSession

        {

            get

            {

                if (this.aspSession == null)

                {

                    this.aspSession = System.Web.HttpContext.Current.Items["HomelidaysAspSession"] as AspSession;

                    if (this.aspSession == null)

                    {

                        throw new Exception("Please make sure that the MyModule1 is configured in web.confg");

                    }

                }

 

                return this.aspSession;

            }

        }

 

    }

}

 

Then in your controller you can access the SessionService object like this:

this.AspSession["DotNetIntValue"] = 12;

For ASP.NET WebForm:

using  Homelidays.Web.SessionService;

/// <summary>
/// The session state : FOR SESSION SERVICE 
/// </summary>
private AspSession aspSession;

/// <summary>
/// Gets the session state : FOR SESSION SERVICE
/// </summary>
public AspSession AspSession
{
    get
  
{
        if (this.aspSession == null)
    
{
            this.aspSession = System.Web.HttpContext.Current.Items["HomelidaysAspSession"] as AspSession;

       if (this.aspSession == null)
      
{
                  throw new Exception("Please make sure that the MyModule1 is configured in web.confg");
      
}
        }

    return this.aspSession;
   }
}

public partial class SessionService_Test : SessionPage
{
   protected void Page_Load(object sender, EventArgs e)
    {
        this.AspSession["DotNetIntValue"] = 12;
        foreach (var item in this.AspSession)
        {
            LContentSess.Text += "Key : " + item.Key + " Value : " + item.Value + "<br/>";
        }

        if (((int)this.AspSession["DotNetIntValue"]) == 12)
        {
            Response.Write("blabla");
        }
    }
}

 

Last edited Nov 10, 2014 at 10:50 AM by Yanal, version 29

Comments

Yanal Jan 27, 2011 at 10:12 PM 
Done !
Thanks for feedback.

edecadoudal Nov 9, 2010 at 7:01 AM 
Is there a Reference to add to the project and a using clause before accessing the this.AspSession object ?