控制器外部的ASP.NET 5 DI应用程序设置 [英] ASP.NET 5 DI app setting outside controller
问题描述
我可以像这样在控制器中进行DI app设置
private IOptions<AppSettings> appSettings;
public CompanyInfoController(IOptions<AppSettings> appSettings)
{
this.appSettings = appSettings;
}
但是如何在这样的自定义类中进行DI
private IOptions<AppSettings> appSettings;
public PermissionFactory(IOptions<AppSettings> appSetting)
{
this.appSettings = appSettings;
}
我在Startup.cs中的注册是
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
正确"的方式
在DI中注册自定义类,就像在ConfigureServices
方法中注册其他依赖项一样,例如:
services.AddTransient<PermissionFactory>();
(代替AddTransient
,您可以使用AddScoped
,或您需要的任何其他生存期)
然后将此依赖项添加到控制器的构造函数中:
public CompanyInfoController(IOptions<AppSettings> appSettings, PermissionFactory permFact)
现在,DI知道了PermissionFactory
,可以实例化它并将其注入到控制器中.
如果要在Configure
方法中使用PermissionFactory
,只需将其添加到其参数列表中即可:
Configure(IApplicationBuilder app, PermissionFactory prov)
Aspnet会发挥神奇的作用,然后将类注入那里.
讨厌"的方式
如果要在代码深处实例化PermissionFactory
,也可以用一些讨厌的方式进行-将对IServiceProvider
的引用存储在Startup
类中:
internal static IServiceProvider ServiceProvider { get;set; }
Configure(IApplicationBuilder app, IServiceProvider prov) {
ServiceProvider = prov;
...
}
现在您可以像这样访问它:
var factory = Startup.ServiceProvider.GetService<PermissionFactory>();
同样,DI将负责将IOptions<AppSettings>
注入到PermissionFactory
中.
I can DI app setting in the controller like this
private IOptions<AppSettings> appSettings;
public CompanyInfoController(IOptions<AppSettings> appSettings)
{
this.appSettings = appSettings;
}
But how to DI that in my custom class like this
private IOptions<AppSettings> appSettings;
public PermissionFactory(IOptions<AppSettings> appSetting)
{
this.appSettings = appSettings;
}
my register in Startup.cs is
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
The "proper" way
Register your custom class in the DI, the same way you register other dependencies in ConfigureServices
method, for example:
services.AddTransient<PermissionFactory>();
(Instead of AddTransient
, you can use AddScoped
, or any other lifetime that you need)
Then add this dependency to the constructor of your controller:
public CompanyInfoController(IOptions<AppSettings> appSettings, PermissionFactory permFact)
Now, DI knows about PermissionFactory
, can instantiate it and will inject it into your controller.
If you want to use PermissionFactory
in Configure
method, just add it to it's parameter list:
Configure(IApplicationBuilder app, PermissionFactory prov)
Aspnet will do it's magic and inject the class there.
The "nasty" way
If you want to instantiate PermissionFactory
somewhere deep in your code, you can also do it in a little nasty way - store reference to IServiceProvider
in Startup
class:
internal static IServiceProvider ServiceProvider { get;set; }
Configure(IApplicationBuilder app, IServiceProvider prov) {
ServiceProvider = prov;
...
}
Now you can access it like this:
var factory = Startup.ServiceProvider.GetService<PermissionFactory>();
Again, DI will take care of injecting IOptions<AppSettings>
into PermissionFactory
.
Asp.Net 5 Docs in Dependency Injection
这篇关于控制器外部的ASP.NET 5 DI应用程序设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!