Changing State Management depending on deployment RRS feed

  • Question

  • User1604699451 posted

    I'm finishing the conversion of an older asp.net web site to an MVP multi-tenant application.   The design uses many session state variables as "global" variables   (i.e. System.Web.HttpContext.Current.Session([variable name]))  The code has not been refactored to change that. 

    I need to be able to run the application both on my local PC IIS and in the cloud (e.g. Azure or AWS).  I would like to be able to use InProc on my local machine and I understand that I probably need to use Redis in the cloud deployment.

    All the session state variables have already been encapsulated in properties, functions or subroutines within a class, so that the variable can be read or written using "SC.VariableName()" type syntax, as in this example

            public static string TestValStr
                get { return HttpContext.Current.Session["TestValStr"].ToString(); }
    set { HttpContext.Current.Session["TestValStr"] = value; }

    I have a plan to change all those variables so that instead of using HttpContext directly they will route through a common routine e.g. "SC.SessionStateVar" (example below).
    (I appreciate that the syntax below may be flawed, so this can be considered pseudo-code; also I recognize I may have to specify type conversion.)

    Public Class SC
    Public Shared Property TestValInt() As Integer
    Dim wrkType As Integer
    Dim wrkVal As Object
    Return SC.SessionStateVar("TestValInt")
    End Get
    Set(value As Integer)
    SC.SessionStateVar("TestValInt") = value
    End Set
    End Property

    Then SC.SessionStateVar would use a config file variable ("CF.StateProvider") to detect and use the appropriate provider (InProc or Redis)

    Public Shared Property SessionStateVar(varValName As String) As Object
    Select Case CF.StateProvider
    Case "Redis"
    'Return Redis_Get(varValName) //--- not sure how the Redis Get is coded
    Case "InProc"
    Return System.Web.HttpContext.Current.Session(varValName)
    End Select
    End Get
    Set(value As Object)
    Select Case GetFromMasterData("StateProvider")
    Case "Redis"
    'Redis_Set = value //--- not sure how the Redis Set is coded
    Case "InProc"
    System.Web.HttpContext.Current.Session(varValName) = value
    End Select
    End Set
    End Property

    I also understand that State provider would also need to be configured properly in the .config file (by having both InProc and Redis and commenting one of them out).

    My questions are, exact syntax aside:

    1) Will this general approach work, i.e. use a config variable to determine which state provider the app should expect?

    2)  Have I made any gross errors in assuming how the syntax would work?  (For instance, is "System.Web.HttpContext.Current.Session(varValName)" the standard way to get to state, for either provider?)

    3) Is there a simpler way to accomplish the same result?

    Any guidance on this would be appreciated.


    Saturday, December 30, 2017 7:25 PM

All replies

  • User283571144 posted

    Hi codequest,

    As far as I know, if we want to use redis or inpro as the session provider, we need change the web config file's setting.

    Like this web config:

    Enable redis:

    <sessionState mode="Custom" customProvider="MySessionStateStore">
        <add name="MySessionStateStore"
               host = "" [String]
            port = "" [number]
            accessKey = "" [String]
            ssl = "false" [true|false]
            throwOnError = "true" [true|false]
            retryTimeoutInMilliseconds = "0" [number]
            databaseId = "0" [number]
            applicationName = "" [String]
            connectionTimeoutInMilliseconds = "5000" [number]
            operationTimeoutInMilliseconds = "5000" [number]
        <add name="MySessionStateStore" type="Microsoft.Web.Redis.RedisSessionStateProvider" host="" accessKey="" ssl="false"/>

    Enable inpro:

    <!-- <sessionState mode="InProc"
            <add name="DefaultSessionProvider"
                        System.Web.Providers, Version=, Culture=neutral,
                  connectionStringName="DefaultConnection" />
    </sessionState> -->

    In my opinion, we could use web config transform. 

    When you want to publish the application to local iis, you could choose local iis web config.

    When you want to o publish the application to azure, you could choose azure web config.

    More details about how to use it, you could refer to this answer.

    Best Regards,


    Friday, January 5, 2018 7:25 AM