Configuring Redis for ASP.NET Core Session Store
2017-02-09 12:03
537 查看
http://www.hossambarakat.net/2016/02/03/configuring-redis-as-asp-net-core-1-0-session-store/
03 Feb 2016 | asp.net
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.
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
a command prompt.
Redis could also be installed as windows service, for more details please review this link
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
At the time of this writing, the package
“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
Update the
to add the Redis caching services as below
The ASP.NET 5 ASP.NET Core
by default,
otherwise you will receive
try to access it.
To use session, add
shown below
Then, Update the
to add the Redis caching services as below
Finally, add the following line to
That’s it, Now you can reference
all session entries will be saved to Redis.
Configuring Redis for ASP.NET Core Session Store
03 Feb 2016 | asp.netAs 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 availablehere
To install Redis on your local machine, install the chocolatey package http://chocolatey.org/packages/redis-64/ and run
redis-serverfrom
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
ConfigureServicesmethod inside the
Startupclass
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 Sessionis not configured
by default,
Sessionconfigurations must be done before using it,
otherwise you will receive
InvalidOperationExceptionwhenever you
try to access it.
To use session, add
Microsoft.AspNet.Sessionto the
project.jsonas
shown below
"dependencies": { "Microsoft.AspNet.Session": "1.0.0-rc1-final" ... },
Then, Update the
ConfigureServicesmethod inside the
Startupclass
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
Sessionfrom
HttpContextand
all session entries will be saved to Redis.
相关文章推荐
- 详解Asp.net Core 使用Redis存储Session
- [转]Asp.net Core 使用Redis存储Session
- Asp.net Core 使用Redis存储Session
- Asp.net Core中使用Redis 来保存Session, 读取配置文件
- Asp.net Core中使用Redis 来保存Session
- ASP.NET Core 使用 Redis 和 Protobuf 进行 Session 缓存
- asp.net core 使用 Redis 和 Protobuf 进行 Session 缓存
- Asp.net Core 使用Redis存储Session
- ASP.NET Core 使用Redis存储Session
- ASP.NET Core 使用Redis存储Session
- Asp.net Core 使用Redis存储Session
- using sql server or sql azure for session state store in asp.net
- ASP.NET MVC 使用Redis共享Session
- [转]ASP.NET HttpModule for handling session end with StateServer
- ASP.NET CORE CACHE的使用(含MemoryCache,Redis)
- 在 ASP.NET CORE 中使用 SESSION
- [转]Using NLog for ASP.NET Core to write custom information to the database
- 详解如何在ASP.NET Core中使用Redis
- asp.net core 使用session
- asp.net core 使用 Redis 和 Protobuf