Search code examples
asp.net-coreredisasp.net-core-2.0servicestack.redis

How To: Register ServiceStack's Redis Client Manager singleton in ASP.NET Core using the default container


I've been reading several documents and articles on how to ServiceStack's Redis client, but all of them use the ServiceStack's AppHost method and their built-in Func IOC But I don't want to mix different IOC containers in my project. And besides I don't want to use any other ServiceStack component other than the Redis client. Therefore I wanted to inject a singleton instance of the IRedisClientsManager preferably through the RedisManagerPool factory, straight from the ConfigureServices method of Startup.cs


Solution

  • After a reviewing the code from the updated .NET Core Live Demos I figured out a clean and simple way of doing it.

    So in my ConfigureServices method I registered IRedisClientsManager like this

    services.AddSingleton<IRedisClientsManager> (c =>
                  new RedisManagerPool(Configuration.GetSection("Redis-Host").Value));
    

    of course in order to read from configuration in ConfigureServices you need to add a constructor to inject it to Startup

    IConfiguration Configuration { get; set; }
            public Startup(IConfiguration configuration) => Configuration = configuration;