.netcore 6 ioc注入的三种方式

发布于:2024-01-10 ⋅ 阅读:(141) ⋅ 点赞:(0)
试读部分
1、定义接口 public interface MyInterceptorInterface 2、实现接口 public class MyInterceptorImpl : MyInterceptorInterface 在构造中增加以下代码,便于观察 static ConcurrentDictionary<string, string> keyValues = new ConcurrentDictionary<string, string>(); public MyInterceptorImpl() { keyValues.TryAdd(Guid.NewGuid().ToString(), "12"); } 3、进行ioc注入 builder.Services.AddTransient<MyInterceptorInterface, MyInterceptorImpl>(); 4、接收对应的注入对象 MyInterceptorInterface myInterceptorInterface; MyAddScoped myAddScoped; public ValuesController(MyInterceptorInterface myInterceptor, MyAddScoped myAddScoped) { myInterceptorInterface = myInterceptor; this.myAddScoped = myAddScoped; } ps:使用[FromServices] 注解,这样也可以在方法中直接获取到,前提是已经注入 public string TestRoute([FromServices] MyInterceptorInterface myInterceptor) 5、调用对应接口 public string TestMyInterce……
本文为 VIP 文章,已展示部分内容,开通 VIP 后查看全文

觉得这篇文章有帮助?

点个赞,或赞赏支持作者持续创作

微信公众号

今日签到

点亮在社区的每一天
去签到