Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Use Strongly-Typed Configuration in Startup Class

$
0
0

Hi,

Is there a way to use strongly typed configuration in startup class? We usually use it in controller or view like below:

public class HomeController : Controller
{
    private readonly AppSettings _settings;

    public HomeController(IOptions<AppSettings> settings)
    {
        _settings = settings.Value;
    }

    public IActionResult Index()
    {
        ViewData["Message"] = _settings.ApplicationTitle;
        return View();
    }
}

however I have a service class that required to be configured in the startup class and has a constructor that requires strongly-typed configuration as parameter as shown below:

public class MyService
{
	private MyOptions _myOptions;

	public MyService(MyOptions myOptions)
	{
		this._myOptions = myOptions;
	}
}

and usually we map the configuration to the strongly-typed class in the Startup.cs like this:

services.Configure<MyOptions>(Configuration.GetSection("MyOptions"));

How do I get the instantiated MyOptions in this code?

Thanks a lot.


Viewing all articles
Browse latest Browse all 9386

Trending Articles