如何使用默认容器在ASP.NET Core中注册ServiceStack的Redis客户端管理对象单例

3
我一直在阅读有关如何使用ServiceStack的Redis客户端的多个文档和文章,但它们都使用了ServiceStack的AppHost方法和其内置的Func IOC。但是我不想在我的项目中混合使用不同的IOC容器。此外,除了Redis客户端之外,我不想使用任何其他ServiceStack组件。因此,我想通过Startup.cs文件中的ConfigureServices方法,注入IRedisClientsManager的单例实例,最好是通过RedisManagerPool工厂直接注入。
1个回答

5
在审查完更新的.NET Core Live Demos代码后,我找到了一种简洁明了的方法。 在我的 ConfigureServices方法中,我按如下方式注册了IRedisClientsManager
services.AddSingleton<IRedisClientsManager> (c =>
              new RedisManagerPool(Configuration.GetSection("Redis-Host").Value));

当然,为了在ConfigureServices中读取配置,您需要添加一个构造函数将其注入到Startup中。
IConfiguration Configuration { get; set; }
        public Startup(IConfiguration configuration) => Configuration = configuration;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接