Sessions stored in a co-located Azure cache gets out of sync on multiple instances
Asked Answered
R

1

6

For my MVC4 application, run in Azure, I store the sessions in a co-located cache. As described in this How-to that Microsoft provide.

I run two small instances, and everything seems to work fine. I can log in to the application, and I remain logged in when I browse around within the application. So the session seem to work on both instances.

However, when I update the session data something like this:

HttpContext.Current.Session["someVar"] = "new value";

That change only seem to have an effect on the instance that handle that particular request. Now as I browse around the application, sometimes I get the initial value and sometimes I get the updated value.

I haven't made any changes to the web.config, so it looks exactly as it do when it gets added by the Nuget package:

<sessionState mode="Custom" customProvider="AppFabricCacheSessionStoreProvider">
  <providers>
    <add name="AppFabricCacheSessionStoreProvider"
          type="Microsoft.Web.DistributedCache.DistributedCacheSessionStateStoreProvider, Microsoft.Web.DistributedCache"
          cacheName="default"
          useBlobMode="true"
          dataCacheClientName="default" />
  </providers>
</sessionState>

Do I need to handle sessions in another way when using the Azure cache, or is it something else I'm missing here?

Rie answered 11/9, 2012 at 10:19 Comment(0)
R
7

You need to assign an applicationName so that the distributed cache can view the shared state within the same application boundary. See MSDN forum post for reference.

<add name="AppFabricCacheSessionStoreProvider"
          type="Microsoft.Web.DistributedCache.DistributedCacheSessionStateStoreProvider, Microsoft.Web.DistributedCache"
          applicationName="azureMVC4App"
          cacheName="default"
          useBlobMode="true"
          dataCacheClientName="default" />

If you want to share cache state across application boundaries, you need to assign sharedId

Reata answered 11/9, 2012 at 13:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.