How to use ConfigurationBinder in Configure method of startup.cs

被刻印的时光 ゝ 提交于 2019-12-02 02:19:27

Every service configured in ConfigureServices can be injected in the Configure method by the runtime:

public void Configure(IApplicationBuilder app, IOptions<AppSettings> options)
{
    // access options.Options here
}

This is a bit cleaner solution than accessing the ServiceProvider directly.

you can get it like this using service locator:

IOptions<AppSettings> settings = app.ApplicationServices.GetService<IOptions<AppSettings>>();
if (settings.Options.whatever)
  {
     ...
  }

I noticed that if you create a new project with the final release of VS 2015 the project template doesn't include AppSettings as the previous project template did, not sure why.

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