How can I retrieve AppSettings configuration back in Asp.Net MVC 6?

后端 未结 1 369
[愿得一人]
[愿得一人] 2021-01-12 13:03

Assuming that I am using the new DepencyInjection framework to configure my classes and dependencies in the new ASP.Net/vNext.

How can I use, How can I get my pre-de

相关标签:
1条回答
  • 2021-01-12 13:57

    You can get AppSettings in your FooService by injecting IOptions<AppSettings> DI service in it's constructor.

    The IOptions<> interface is part of something called Options Model which is used for accessing POCO style settings(ex: your AppSettings) across your application. The calls like services.Configure<AppSettings>( and services.Configure<FacebookAuthenticationOptions>(options => in your above example, actually register DI services which in turn are used by a DI service called OptionsManager when resolving requests for IOptions<>.

    Example:

    public class FooService
    {
        private readonly AppSettings _settings;
    
        public FooService(IOptions<AppSettings> options)
        {
            _settings = options.Options;
        }
    
        ....
        ....
    }
    
    0 讨论(0)
提交回复
热议问题