简体   繁体   English

存储在共存Azure缓存中的会话在多个实例上不同步

[英]Sessions stored in a co-located Azure cache gets out of sync on multiple instances

For my MVC4 application, run in Azure, I store the sessions in a co-located cache. 对于我的MVC4应用程序,在Azure中运行,我将会话存储在共存的缓存中。 As described in this How-to that Microsoft provide. 如Microsoft提供的本方法文档中所述。

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: 我没有对web.config进行任何更改,因此它看起来与Nuget包添加时完全一样:

<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? 在使用Azure缓存时,是否需要以其他方式处理会话,或者这是我在这里缺少的其他内容?

You need to assign an applicationName so that the distributed cache can view the shared state within the same application boundary. 您需要分配applicationName以便分布式缓存可以查看同一应用程序边界内的共享状态。 See MSDN forum post for reference . 请参阅MSDN论坛帖子以供参考

<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 如果要跨应用程序边界共享缓存状态,则需要分配sharedId

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM