In ASP.NET Core, the built-in dependency injection system is a godsend for many developers. One lesser-known feature is the ability to inject services defined in ConfigureServices directly into the Configure method. This article will walk you through the process of achieving this and highlight its benefits.

Setting the Stage: Our Custom Service

To start, let’s assume you have an application where you need to fetch some settings dynamically. We’ll create an interface ISettingsService and its implementation:

public interface ISettingsService
{
    string GetSetting(string key);
}

public class SettingsService : ISettingsService
{
    public string GetSetting(string key)
    {
        // This is a mock-up; in reality, you might fetch this from a database or a config file.
        return $"Value for {key}";
    }
}

Service Registration in ConfigureServices

Before a service can be used, it has to be registered with the dependency injection container. In the Startup class, this is usually done within the ConfigureServices method:

public void ConfigureServices(IServiceCollection services)
{
    // ... other service registrations ...

    services.AddSingleton<ISettingsService, SettingsService>();
}

Here, we’ve registered our SettingsService as a singleton, meaning a single instance will be created and shared across the application.

Injecting Our Custom Service into Configure

With the service registered, you can now inject it into the Configure method:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ISettingsService settingsService)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
    }

    // Harnessing the power of our injected service
    var someSetting = settingsService.GetSetting("SomeKey");
    // Based on the setting, you can adjust your middleware or handle requests differently.

    app.UseStaticFiles();
    app.UseRouting();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapDefaultControllerRoute();
    });
}

In the Configure method, the custom service, settingsService, is now available for use, just like any other dependency.

Benefits and Scenarios

  • Dynamic Configuration: If your application’s behavior depends on some configurations that change dynamically (e.g., feature toggles, dynamic routes, etc.), this approach can be invaluable.
  • Service Layer Interaction: Fetching settings might involve interacting with databases, cache, or other services. Encapsulating this within a service keeps the Configure method clean and adheres to the Single Responsibility Principle.
  • Reusability: The same service can be injected elsewhere, be it controllers, other services, or view components, ensuring a consistent approach to fetching settings.

Conclusion

ASP.NET Core’s flexibility shines through its powerful yet straightforward dependency injection mechanism. As demonstrated, registering services in ConfigureServices and then harnessing them in the Configure method is straightforward. This not only provides developers with dynamic configuration options but also promotes clean code and reusability.

Advertisements

Leave a comment

Recent posts

Advertisements

Quote of the week

“People ask me what I do in the winter when there’s no baseball. I’ll tell you what I do. I stare out the window and wait for spring.”

~ Rogers Hornsby

Designed with WordPress