locked
Session variable setting in ThreadPool.QueueUserWorkItem not updating when using Azure cache for seesion state RRS feed

  • Question

  • I am trying to find out if there is an inherent problem with using session variables with Azure cache.

    I have a controller that set a sesion variable using

    Session["UpdateStatus"]="Just Started"

    I then call my long running process

    ThreadPool.QueueUserWorkItem(delegate{MyLongProcess()});

    and in there I update the session variable with status update info, which I then feed back to an ajax page etc..

    All worked great. However, since I implemented the Azure caching for session state (rather than default InProc), the threaded process does not seem to be able to update the same session state variables, so the controller never gets updates.

    I am starting a work around to use straight cache access using Gets and Puts, but I now need to manage the separation of users variable myself etc..

    Anyone tried this, or know if it's by design or just another thing missing from Azure?

    Thursday, April 19, 2012 1:19 AM

Answers

  • Hi,

    If you only want to store update state in your application, why not use DataCache? Following code shows how to store state variable in Cache service:

    Web.Config

     <configSections>
        <section name="dataCacheClients" type="Microsoft.ApplicationServer.Caching.DataCacheClientsSection, Microsoft.ApplicationServer.Caching.Core"
                 allowLocation="true" allowDefinition="Everywhere"/>
      </configSections>
     <dataCacheClients>
        <dataCacheClient name="default">
          <hosts>
            <host name="<Your cache service url> cachePort="22233" />
          </hosts>
          <securityProperties mode="Message">
            <messageSecurity        authorizationInfo="<Your Token>">
            </messageSecurity>
          </securityProperties>
        </dataCacheClient>
      </dataCacheClients>

    Code in Default.aspx.cs

            DataCacheFactory factory =new DataCacheFactory();
            DataCache defaultCache;
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {               
    
                    defaultCache = factory.GetDefaultCache();
                    defaultCache.Put("state", "Just Started");
                    ThreadPool.QueueUserWorkItem(delegate { MyLongProcess(); });
    
                }
            }
    
            private void MyLongProcess()
            {
                // just use to monitor long code logic.
                Thread.Sleep(5000); 
                defaultCache.Put("state", "In Process");
            }
    
            protected void Button1_Click(object sender, EventArgs e)
            {
                defaultCache = factory.GetDefaultCache();
                object o = defaultCache["state"];
                Response.Write(o);
            }

    Hope this helps.

    Please mark the replies as answers if they help or unmark if not. If you have any feedback about my replies, please contact msdnmg@microsoft.com Microsoft One Code Framework


    Thursday, April 19, 2012 6:02 AM
  • I hope both operations adding and updating session variable is using the same configuration for SessionProvider using AppFabric Cache. Try to explicitly set the application name in the session provider setting for app fabric cache.

    Also, would you mind sharing the configuration and how are you trying to update session variable it from longruning process?


    -Sachin Sancheti

    • Marked as answer by Arwind - MSFT Wednesday, April 25, 2012 5:55 AM
    Thursday, April 19, 2012 10:13 AM

All replies

  • Hi,

    If you only want to store update state in your application, why not use DataCache? Following code shows how to store state variable in Cache service:

    Web.Config

     <configSections>
        <section name="dataCacheClients" type="Microsoft.ApplicationServer.Caching.DataCacheClientsSection, Microsoft.ApplicationServer.Caching.Core"
                 allowLocation="true" allowDefinition="Everywhere"/>
      </configSections>
     <dataCacheClients>
        <dataCacheClient name="default">
          <hosts>
            <host name="<Your cache service url> cachePort="22233" />
          </hosts>
          <securityProperties mode="Message">
            <messageSecurity        authorizationInfo="<Your Token>">
            </messageSecurity>
          </securityProperties>
        </dataCacheClient>
      </dataCacheClients>

    Code in Default.aspx.cs

            DataCacheFactory factory =new DataCacheFactory();
            DataCache defaultCache;
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {               
    
                    defaultCache = factory.GetDefaultCache();
                    defaultCache.Put("state", "Just Started");
                    ThreadPool.QueueUserWorkItem(delegate { MyLongProcess(); });
    
                }
            }
    
            private void MyLongProcess()
            {
                // just use to monitor long code logic.
                Thread.Sleep(5000); 
                defaultCache.Put("state", "In Process");
            }
    
            protected void Button1_Click(object sender, EventArgs e)
            {
                defaultCache = factory.GetDefaultCache();
                object o = defaultCache["state"];
                Response.Write(o);
            }

    Hope this helps.

    Please mark the replies as answers if they help or unmark if not. If you have any feedback about my replies, please contact msdnmg@microsoft.com Microsoft One Code Framework


    Thursday, April 19, 2012 6:02 AM
  • I hope both operations adding and updating session variable is using the same configuration for SessionProvider using AppFabric Cache. Try to explicitly set the application name in the session provider setting for app fabric cache.

    Also, would you mind sharing the configuration and how are you trying to update session variable it from longruning process?


    -Sachin Sancheti

    • Marked as answer by Arwind - MSFT Wednesday, April 25, 2012 5:55 AM
    Thursday, April 19, 2012 10:13 AM