Redis Configuration for Distributed Caching on .NET Core 1.1

Redis Configuration for Distributed Caching on .NET Core 1.1

project.json

Have these entries under the dependencies section

  {
    "dependencies": {
      ...
      "Microsoft.AspNetCore.Session": "1.1.0",
      "Microsoft.Extensions.Caching.Redis": "1.1.0",
      ...
    },
  }

Startup.cs

It is important that app.UseSession() under the Configure() method appears before app.UseMvcWithDefaultRoute() otherwise your application will throw a Session error.

  ...
  public void ConfigureServices(IServiceCollection services)
  {
      ...
      services.AddDistributedRedisCache(options =>
      {
          options.Configuration = "127.0.0.1:PORT_NUMBER";
          options.InstanceName = "ARBITRARY_REDIS_INSTANCE_NAME";
      });
      services.AddSession();
  }

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

Usage

These can be in any entity provided they have access to the Session object.

  // Setter
  HttpContext.Session.SetString("string_test", "Test string at " + 
                                  DateTime.Now.ToString());

  // Getter
  HttpContext.Session.GetString("string_test")