Sessions stored in Azure shared cache go out of sync across multiple instances

In my MVC4 application running on Azure, I store sessions in a neighboring cache. As described in this Microsoft-provided How-to .

I run two small instances and everything works fine. I can log in to the application and I stay logged in when I view the application. So the session seems to work in both cases.

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

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

This change only affects the instance processing this particular request. Now, when I browse the application, sometimes I get the initial value, and sometimes I get the updated value.

I did not make any changes to the web.config file, so it looks exactly the same as when adding 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 the sessions in a different way when using the Azure cache, or is this something else I am missing here?

+6
source share
1 answer

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

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

If you want to split the cache state between application boundaries, you need to assign sharedId

+7
source

Source: https://habr.com/ru/post/925112/


All Articles