Home > Backend Development > C++ > How Can I Manually Resolve Services within ASP.NET Core's `ConfigureServices` Method?

How Can I Manually Resolve Services within ASP.NET Core's `ConfigureServices` Method?

Barbara Streisand
Release: 2025-01-17 08:46:08
Original
366 people have browsed it

How Can I Manually Resolve Services within ASP.NET Core's `ConfigureServices` Method?

Manually resolve the service in the ConfigureServices method of ASP.NET Core

ASP.NET Core uses the IServiceCollection interface to build a dependency injection (DI) container. Once built, it is composed into a IServiceProvider instance that allows you to resolve services. This article guides how to manually parse instances in the ConfigureServices method.

Inject dependencies

The

ConfigureServices method does not allow service injection, so you can inject services into the constructor of the Startup class, such as IConfiguration, IWebHostEnvironment and ILoggerFactory.

<code class="language-csharp">public Startup(IConfiguration configuration) => Configuration = configuration;

public void ConfigureServices(IServiceCollection services) => services.AddScoped<IFooService>();</code>
Copy after login

Manually resolve dependencies in Configure()

You can manually resolve the service in the IApplicationBuilder method using the ApplicationServices attribute of Configure():

<code class="language-csharp">public void Configure(IApplicationBuilder app)
{
    var serviceProvider = app.ApplicationServices;
    var fooService = serviceProvider.GetService<IFooService>();
}</code>
Copy after login

Manually resolve dependencies in ConfigureServices()

To resolve services in ConfigureServices, use BuildServiceProvider() to construct an intermediate IServiceProvider to access services registered up to that point:

<code class="language-csharp">public void ConfigureServices(IServiceCollection services)
{
    services.AddSingleton<IFooService>();
    var sp = services.BuildServiceProvider();
    var fooService = sp.GetService<IFooService>();
}</code>
Copy after login

Warning:

Avoid resolving services in ConfigureServices as it should focus on configuring application services. Consider manually binding the value from IConfiguration to the instance, or using overloads of AddSingleton/AddScoped/AddTransient to lazily create the instance.

Manually resolving services (service locators) is an anti-pattern and should be used with caution.

The above is the detailed content of How Can I Manually Resolve Services within ASP.NET Core's `ConfigureServices` Method?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template