如何在业务类中注入IDistributedCache

我有一个asp.net核心项目,我正在尝试使用IDistributedCache(Redis)。

我想在自定义类中访问IDistributedCache,而不仅是在控制器中。

有可能吗?

ppsda 回答:如何在业务类中注入IDistributedCache

  

我想在自定义类中访问IDistributedCache,而不仅是在控制器中。有可能吗?

是的

注册服务集合

var redisconnection = "...";
services.AddDistributedRedisCache(o => { o.Configuration = redisconnection; });
services.AddScoped<MyCustomClass>();

//...

并将其作为自定义类的可注入参数

public MyCustomClass(IDistributedCache cache) {
    //...
}
本文链接:https://www.f2er.com/3164761.html

大家都在问