多区域 ASP.NET Core 会话提供者
NCache 提供ASP.NET Core session-sharing support across multiple regions. This prevents users from losing their sessions in case they need to reroute their traffic to another location because of traffic or due to disaster recovery. Sessions are replicated across WAN seamlessly without replicating the entire session which may result in bandwidth expense.
图 1:跨数据中心的会话共享
To share ASP.NET Core session across regions, you need to enable location affinity. This is enabled by setting the EnableLocationAffinity flag to true and providing the cache names and prefixes to identify the source (location) of the cache for the request. You can provide multiple cache names that form the web farm, however, the cache specified against CacheName is considered the primary cache by default.
Prerequisites to Configure Multi-Region ASP.NET Core 会话提供者
- 根据您的应用程序在您的应用程序中安装以下 NuGet 包 NCache 版本:
- 要使用该扩展,请在您的应用程序中包含以下命名空间 启动文件:
- 缓存必须正在运行。
- 有关 API 详细信息,请参阅: 地址NCache时间, 使用NCache时间.
- 确保要添加的数据是 可序列化.
- 为确保操作是故障安全的,建议处理应用程序中的任何潜在异常,如中所述 处理故障.
- 要处理任何看不见的异常,请参阅 故障排除 部分。
第 1 步:配置服务
会话管理服务需要初始化。 在 启动文件,使用 AddNCacheSession()
扩展方法 IServiceCollection
in ConfigureServices()
初始化服务。 这个扩展方法的最基本的重载需要一个 IOptions<NCacheSessionConfiguration>
作为其基本配置。
有两种方法可以指定配置:
- 通过您的应用程序 Startup.cs 或
- 在 JSON 格式中 应用设置.json .
方法一:在 Startup.cs 中指定配置
要启用会话共享,需要设置 EnableLocationAffinity
标记为 true 并提供缓存列表 AffinityMapping
。 请注意,其中一个缓存应与 CacheName 匹配,这被视为主缓存。 还需要指定两个应用程序共享的目录。
备注
缓存前缀的长度最多为 4 个字符。
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc();
services.AddDataProtection()
.PersistKeysToFileSystem(new DirectoryInfo(dir))
.SetApplicationName("SharedCookieApp");
services.AddNCacheSession(configuration =>
{
configuration.CacheName = "LondonCache"; //compulsory
configuration.EnableLogs = true;
configuration.SessionOptions.IdleTimeout = 5;
configuration.EnableLocationAffinity = true;
configuration.AffinityMapping = new CacheAffinity[]
{
new CacheAffinity { CacheName = "LondonCache", CachePrefix = "LDNC"},
new CacheAffinity { CacheName = "NewYorkCache", CachePrefix = "NYKC"},
new CacheAffinity { CacheName = "TokyoCache", CachePrefix = "TKYC"},
};
});
}
方法二:在 Appsettings.json 中指定配置
会话管理服务需要初始化。 在 启动文件,使用 AddNCacheSession()
扩展方法 IServiceCollection
in ConfigureServices()
初始化服务。 这个扩展方法的最基本的重载需要一个 IOptions<NCacheSessionConfiguration>
作为其基本配置。
备注
缓存前缀的长度最多为 4 个字符。
"NCacheSettings": {
"EnableLocationAffinity": true,
"EnableLogs": true,
"RequestTimeout": 5,
"CacheName": "LondonCache",
"AffinityMapping": [
{
"CacheName": "LondonCache",
"CachePrefix": "LDNC"
},
{
"CacheName": "NewYorkCache",
"CachePrefix": "NYKC"
},
{
"CacheName": "TokyoCache",
"CachePrefix": "TKYC"
}
]
}
public void ConfigureServices(IServiceCollection services)
{
//Add framework services
services.AddMvc();
//Add services to the container
services.AddNCacheSession(Configuration.GetSection("NCacheSettings"));
}
第 2 步:将中间件添加到应用程序
服务初始化后,您可以通过在中添加中间件来配置 HTTP 请求管道 Configure()
方法。 以下是显示通过使用添加中间件层的摘录 UseNCacheSession()
扩展方法在 IApplicationBuilder
。 该 NCache 会话中间件应始终堆叠在使用会话的层之前。
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
if (env.IsDevelopment()){
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
}
else{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseNCacheSession(); //store NCache session data
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=myApp}/{action=Index}/{id?}");
});
}
您现在可以继续 使用 NCache 作为底层缓存 为您的 ASP.NET Core 会话。
更多资讯
NCache 提供了会话缓存的示例应用程序 GitHub上.
- 附带 NCache: %NCHOME%\samples\dotnetcore\SessionCaching
参见
.NET: Alachisoft.NCache.Web.会话状态 命名空间。