Configuration class for Session Data Management Tool

I am using Session Data Management Tool created by Xiangyang Liu to store data between different threads in ASP.NET asyncronous application.

The source code of the tool has SessionService web site where configuration is done in global.asax.

To make it easier to incorporate it into existing  ASP.Net web application, it was moved to a separate class. The changes also were done to store trace and data folders in Temp directory, which is easier for deployment.

Option Strict On

Option Explicit On

 

Imports Microsoft.VisualBasic.ControlChars

Imports System.Threading

Imports System.Runtime.Serialization

Imports System.Runtime.Serialization.Formatters.Binary

Imports System.IO

Imports Tools.TraceUtility

Imports System.Configuration ‘mnf 21/9/2005

‘Imports System.Web ‘mnf 21/9/2005 Added reference

 

Public Class SessionManagerStartEnd

    ‘Ideally it will be good to pass HttpSessionState.Timeout to make it the same as  SessionManager.Timeout

    ‘However HttpSessionState.Timeout is known on Session_Start, not Application_Start event

    ‘TODO extract HttpSessionState.Timeout from Web,config in  Application_Start

    Public Shared Sub ApplicationStart(ByVal VirtDir As String) ‘ByVal SessionTimeout As Integer) ‘ByVal Server As HttpServerUtility)

        Dim sRootFolder As String ‘= Server.MapPath(“.”) + “..”

        Dim sTraceFilePrefix As String = ConfigurationSettings.AppSettings.Get(“TraceFilePrefix”)

        If sTraceFilePrefix Is Nothing OrElse sTraceFilePrefix = “” Then

            sTraceFilePrefix = “LogSessionService”

        End If

        If sTraceFilePrefix.IndexOf(“:”) Then

            sTraceFilePrefix = GetPathInTempFolder(VirtDir, sTraceFilePrefix) ‘sRootFolder + sTraceFilePrefix

        End If

        SetTraceFilePrefix(sTraceFilePrefix)

        Dim sTraceLevel As String = ConfigurationSettings.AppSettings.Get(“TraceLevel”)

        If sTraceLevel Is Nothing OrElse sTraceLevel = “” Then

            sTraceLevel = “40”

        End If

        SetTraceLevel(CInt(sTraceLevel))

        Dim sTraceCleanup As String = ConfigurationSettings.AppSettings.Get(“TraceCleanup”)

        If sTraceCleanup Is Nothing OrElse sTraceCleanup = “” Then

            sTraceCleanup = “7”

        End If

        SetCleanupSchedule(CInt(sTraceCleanup))

        WriteTrace(30, “SessionService started”)

        Dim mgr As SessionManager = New SessionManager

        Dim sFilePath As String = ConfigurationSettings.AppSettings.Get(“SessionDataFile”)

        If sFilePath Is Nothing OrElse sFilePath = “” Then

            sFilePath = “DataSessionDataFile”

        End If

        If sFilePath.IndexOf(“:”) Then

            sFilePath = GetPathInTempFolder(VirtDir, sFilePath) ‘ sRootFolder + sFilePath

        End If

        mgr.SetSessionDataFile(sFilePath)

        Dim sTimeout As String = ConfigurationSettings.AppSettings.Get(“Timeout”)

        If Not sTimeout Is Nothing AndAlso sTimeout “” Then

            mgr.SetTimeout(CInt(sTimeout))

        End If

        ‘If SessionTimeout > 0 Then

            mgr.SetTimeout(SessionTimeout)

        ‘End If

        mgr.OnStart()

    End Sub

    Public Shared Function GetPathInTempFolder(ByVal VirtDir As String, ByVal RelativeFolderName As String) As String

        Dim sTempFolderName As String = Path.GetTempPath()         Environment.GetEnvironmentVariable(“TEMP”)

        sTempFolderName = Path.Combine(sTempFolderName, VirtDir & “SessionTools”)

        Dim pathTarget As String = Path.Combine(sTempFolderName, RelativeFolderName)

        Dim sTargetDir As String = Path.GetDirectoryName(pathTarget)

        If (Not Directory.Exists(sTargetDir)) Then

            Directory.CreateDirectory(sTargetDir)

        End If

        Return pathTarget

    End Function

 

    Public Shared Sub ApplicationEnd()

        Dim mgr As SessionLib.SessionManager = New SessionLib.SessionManager

        mgr.OnStop()

        WriteTrace(30, “SessionService ended”)

    End Sub

 

 

End Class