Configuring Redis for ASP.NET Core Session Store

http://www.hossambarakat.net/2016/02/03/configuring-redis-as-asp-net-core-1-0-session-store/

Configuring Redis for ASP.NET Core Session Store

As you may have seen in this ASP.NET Community Standup and Scott Hanselman’s blog post ASP.NET 5 is dead - Introducing ASP.NET Core 1.0 and .NET Core 1.0, ASP.NET 5 is being renamed to ASP.NET Core so I will use the new names in this post.

Redis is an open source (BSD licensed), in-memory data structure store, used as database, cache and message broker. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries. Redis works with an in-memory dataset. it also supports persisting the dataset to disk. Moreover, It provides master-slave asynchronous replication. For more details, check the documentation.

This post will go through the steps required to configure Redis for ASP.NET 5 ASP.NET Core session store targeting DNX on .NET Framework 4.5.1 - 4.6.

Installing Redis

Redis is not officially supported on windows. However, the Microsoft Open Tech group develops and maintains Windows port targeting Win64 available here

To install Redis on your local machine, install the chocolatey package http://chocolatey.org/packages/redis-64/ and run redis-server from a command prompt.

Redis could also be installed as windows service, for more details please review this link

Configuring Distributed Cache

ASP.NET shipped with several caching implementations including Redis, To use ASP.NET 5 ASP.NET Core distributed caching, add the following packages to your project.json

At the time of this writing, the package Microsoft.Extensions.Caching.Redisversion “1.0.0-rc1-final” doesn’t support .NET Core 1.0 however it is planned to be supported and for more details check this issue

"dependencies": {
    "Microsoft.Extensions.Caching.Redis": "1.0.0-rc1-final"
    ...
  },

Update the ConfigureServices method inside the Startup class to add the Redis caching services as below

public void ConfigureServices(IServiceCollection services)
{
    services.AddSingleton<IDistributedCache>(
                serviceProvider =>
                    new RedisCache(new RedisCacheOptions
                    {
                        Configuration = "localhost",
                        InstanceName = "Sample"
                    }));
    //Other services configuration...
}

Configuring Session

The ASP.NET 5 ASP.NET Core Session is not configured by default, Sessionconfigurations must be done before using it, otherwise you will receive InvalidOperationException whenever you try to access it.

To use session, add Microsoft.AspNet.Session to the project.json as shown below

"dependencies": {
    "Microsoft.AspNet.Session": "1.0.0-rc1-final"
    ...
  },

Then, Update the ConfigureServices method inside the Startup class to add the Redis caching services as below

public void ConfigureServices(IServiceCollection services)
{
    services.AddSession();
    //Other services configuration...
}

Finally, add the following line to Configure:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    app.UseSession();
}

That’s it, Now you can reference Session from HttpContext and all session entries will be saved to Redis.


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值