类中的ASP.NET 5 MVC 6配置 [英] ASP.NET 5 MVC 6 Configuration in Class
问题描述
如何从不是控制器且无法在构造函数中接收IOptions的非静态类访问appsettings.json中的配置?
How can the configurations in appsettings.json be accessed from a non-static class that is not a controller and can't receive IOptions in the constructor?
推荐答案
可以使用属性或方法注入吗?并允许您更改默认容器吗?如果是这样:
Can you use property or method injection? And are you allowed to change the default container? If so:
首先将默认容器更改为例如Autofac(在project.json中引用Autofac.Extensions.DependencyInjection版本4.0.0-rc1-177).如下更改您的ConfigureServices:
First change your default container to for instance Autofac (reference Autofac.Extensions.DependencyInjection version 4.0.0-rc1-177 in your project.json). Change your ConfigureServices as follows:
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddMvc();
var builder = new ContainerBuilder();
builder.Populate(services);
var container = builder.Build();
return container.Resolve<IServiceProvider>();
}
使用Autofac通过属性或方法注入来连接您的类.您可以在此处阅读如何: http://docs.autofac. org/en/latest/register/prop-method-injection.html
Use Autofac to wire up your class via property or method injection. You can read here how to: http://docs.autofac.org/en/latest/register/prop-method-injection.html
这篇关于类中的ASP.NET 5 MVC 6配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!