Instantiate a Service in ConfigureServices Method in .NET Core
.NET Core is built in with dependency injection. Usually method ConfigureServices in Startup class is used to register services in the container.
The signature of the method looks like the following:
public void ConfigureServices(IServiceCollection services)
{}
Sometimes it may be necessary to instantiate a service in this function. To do that, you can use the following method:
var sp = services.BuildServiceProvider();
var dbConfig = sp.GetService<IOptions<DatabaseConfig>>().Value;
In the above example, the BuildServiceProvider function is invoked to create a service provider object which then is used to get service.
info Last modified by Raymond 4 years ago
copyright
This page is subject to Site terms.
comment Comments
No comments yet.
Log in with external accounts
warning Please login first to view stats information.
article
Retrieve Http client request metadata like IP address and languages in asp.net core
article
Fix Error CryptographicException: The payload was invalid.
article
Query JSON via JMESPath in .NET
article
SQLite in .NET Core with Entity Framework Core
article
.NET for Apache Spark Preview with Examples
Read more (37)