ASP.NET 5 MVC 6 Configuration in Class

最后都变了- 提交于 2019-12-11 12:36:32

问题


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?


回答1:


Can you use property or method injection? And are you allowed to change the default container? If so:

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>();
}

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



来源:https://stackoverflow.com/questions/36071459/asp-net-5-mvc-6-configuration-in-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!