gpt4 book ai didi

c# - 如何在业务类中注入(inject)IDistributedCache

转载 作者:行者123 更新时间:2023-12-02 10:44:32 30 4
gpt4 key购买 nike

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

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

这可能吗?

最佳答案

I want to access IDistributedCache in a custom class, not only in a controller. Is that possible?

是的。

注册服务集合

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

//...

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

public MyCustomClass(IDistributedCache cache) {
//...
}

关于c# - 如何在业务类中注入(inject)IDistributedCache,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58698578/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com