问题
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