非控制器的网络核心依赖注入 [英] Net Core Dependency Injection for Non-Controller
本文介绍了非控制器的网络核心依赖注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
看起来如此之类的事情令我感到头痛,这似乎很疯狂.但是这里是:
Seems crazy that something like this is causing me such a headache. But here it is:
对于非控制器类,如何对网络核心使用内置的依赖项注入?请提供一个包含包含实例化的示例.
How do you use the built-in dependency injection for net core for a non-controller class? Please provide an example with includes instantiation.
谢谢.
推荐答案
只需将类作为服务即可.
Just make the class a service.
在startup.cs
In startup.cs
services.AddScoped<AccountBusinessLayer>();
然后在控制器中,与对其他服务的操作相同:
Then in controller, same as you do for other services:
private readonly AccountBusinessLayer _ABL;
像处理其他服务一样包含在构造函数中:
Include in constructor as you do for other services:
public AccountController(
UserManager<ApplicationUser> userManager,
SignInManager<ApplicationUser> signInManager,IOptions<IdentityCookieOptions> identityCookieOptions,
IEmailSender emailSender,
ISmsSender smsSender,
ILoggerFactory loggerFactory,
RoleManager<IdentityRole> roleManager,
AccountBusinessLayer ABL
)
{
_userManager = userManager;
_signInManager = signInManager;
_externalCookieScheme = identityCookieOptions.Value.ExternalCookieAuthenticationScheme;
_emailSender = emailSender;
_smsSender = smsSender;
_logger = loggerFactory.CreateLogger<AccountController>();
_roleManager = roleManager;
_ABL = ABL;
}
这篇关于非控制器的网络核心依赖注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文