本文介绍了非控制器的 Net Core 依赖注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
像这样的事情让我如此头疼,这似乎很疯狂.但它是:
Seems crazy that something like this is causing me such a headache. But here it is:
对于非控制器类,如何使用net core的内置依赖注入?请提供包含实例化的示例.
How do you use the built-in dependency injection for net core for a non-controller class? Please provide an example with includes instantiation.
谢谢.
推荐答案
让课程成为一项服务.
在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;
}
这篇关于非控制器的 Net Core 依赖注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!