01.Download latest redis from download ,install and start the redis service from services.msc
02.Add two library in project.json
"Microsoft.Extensions.Caching.Redis.Core": "1.0.3",
"Microsoft.AspNetCore.Session": "1.1.0",
03.Add you dependency injection in
public void ConfigureServices(IServiceCollection services)
{
services.AddApplicationInsightsTelemetry(Configuration);
services.AddMvc();
//For Redis
services.AddSession();
services.AddDistributedRedisCache(options =>
{
options.InstanceName = "Sample";
options.Configuration = "localhost";
});
}
and in Configure
method add top of app.UseMvc line
app.UseSession();
to use redis in session storage in asp.net core .Now you can use like this in HomeController.cs
public class HomeController : Controller
{
private readonly IDistributedCache _distributedCache;
public HomeController(IDistributedCache distributedCache)
{
_distributedCache = distributedCache;
}
//Use version Redis 3.22
//https://mcmap.net/q/921417/-redissessionstateprovider-err-unknown-command-39-eval-39
public IActionResult Index()
{
_distributedCache.SetString("helloFromRedis", "world");
var valueFromRedis = _distributedCache.GetString("helloFromRedis");
return View();
}
}
Microsoft.Extensions.Caching.Redis
instead, which is out-of-the-box redis support for distributed caching? It's one of the 3 default implementations ofIDistrubutedCache
interface github.com/aspnet/Caching/tree/1.0.0/src – WolfishStackExchange.Redis
only contains a redis client, it do not implement itself into ASP.NET Core. But Microsofts distributed caching implementation uses
Microsoft.Extensions.Caching.Redis, its just a wrapper around it and the
IDistrubtedCache` interface. github.com/aspnet/Caching/blob/dev/src/Microsoft.Extensions.Caching.Redis/RedisCache.cs – WolfishMicorosoft.Extensions.Caching.Abstratctions
project, since Redis class library references it. However I ran into problems, since I cannot resolve one of the dependencies, check this question – Frostbite