c# - 如何在业务类中注入 IDistributedCache
问题描述
我有一个 asp.net 核心项目,我正在尝试使用IDistributedCache
(Redis)。
我想IDistributedCache
在自定义类中访问,而不仅仅是在控制器中。
那可能吗?
解决方案
我想在自定义类中访问 IDistributedCache,而不仅仅是在控制器中。那可能吗?
是的。
注册服务集合
var redisconnection = "...";
services.AddDistributedRedisCache(o => { o.Configuration = redisconnection; });
services.AddScoped<MyCustomClass>();
//...
并将其作为自定义类的可注入参数
public MyCustomClass(IDistributedCache cache) {
//...
}
推荐阅读
- sql - 在配置单元表中解析字符串
- group-by - How to group by columns?
- javascript - 使用 .parent() 和 .closest() 单击/更改时的 JQuery 基本文本替换
- html - 使用 background-image:url 与图像标签居中的图像
- java - 使用 JSoup 上的“发布”请求登录并从网站检索数据
- conemu - 在“创建新控制台”对话框中更改“新进程的启动目录”的默认值
- vue.js - 如何使用带有 vue highcharts 的热图?
- objective-c - 目标 c - for 循环按顺序从 iCloud 获取图像
- c# - 如何使用在 razor 中填充的选择来修复我的搜索
- json - 如何使用 JSON 返回数字和换行符