I am facing session expire issue when load balancer switches the server of my application.
I want to share session in between 2 servers through Load balancer in Asp.net Core can any one suggest me how to implement inproc session?
Thanks.
I am facing session expire issue when load balancer switches the server of my application.
I want to share session in between 2 servers through Load balancer in Asp.net Core can any one suggest me how to implement inproc session?
Thanks.
InProc session will not help you, since it resides only on the web server. If you switch servers due to loadbalancing or fail over, you lose the session. What you are looking for is an IDistributedCache
. Examples using Redis or SQL can be found in the docs (Seriously, read them. They are awesome!)
If you want to use Redis you need to install the package for it first
Install-Package Microsoft.Extensions.Caching.Redis
After that, configure it on your ConfigureServices
method:
public void ConfigureServices(IServiceCollection services)
{
services.AddDistributedRedisCache(options =>
{
options.Configuration = "localhost";
options.InstanceName = "SampleInstance";
});
}
Then add the .UseSession
to your Configure
method:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
/*... omitted ...*/
app.UseCookiePolicy();
app.UseSession();
app.UseHttpContextItemsMiddleware();
app.UseMvc();
}
}