Studio.Providers 1.1.0

dotnet add package Studio.Providers --version 1.1.0
                    
NuGet\Install-Package Studio.Providers -Version 1.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Studio.Providers" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Studio.Providers" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="Studio.Providers" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Studio.Providers --version 1.1.0
                    
#r "nuget: Studio.Providers, 1.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Studio.Providers@1.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Studio.Providers&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=Studio.Providers&version=1.1.0
                    
Install as a Cake Tool

Service Providers

Introduction

As ASP.NET Core applications grow, organizing startup logic can become challenging. Configuration code scattered across Program.cs quickly becomes difficult to maintain, test, and understand. Service providers offer an elegant solution to this problem by providing a clean, modular approach to configuring applications.

Service providers are the central place of all service container bindings in an application. They allow grouping related functionality together, making an application's bootstrapping process more organized and maintainable.

All service providers extend the Studio.Providers.ServiceProvider base class and implement a two-step initialization process through the Register and Boot methods.

The Register method is exclusively responsible for binding services into the service container. Within this method, nothing else should be done besides registering service bindings. Otherwise, services bound by providers that have not loaded yet may accidentally be used.

After all providers have registered their services, the Boot method is called on each provider. At this point, the application has been fully constructed and all services are available, allowing providers to configure middleware, routing, and other application features.

Installation

Studio.Providers may be installed via the .NET CLI:

dotnet package add Studio.Providers

Writing Service Providers

All service providers extend the Studio.Providers.ServiceProvider class. Most providers contain a Register and a Boot method. The Register method is called first, followed by the Boot method once all providers have been registered.

The Register Method

Within the Register method, services should only be bound into the IServiceCollection. The Register method is called before the application is built, meaning the WebApplication instance does not exist yet and middleware or routing cannot be configured at this stage:

namespace App.Providers;

public class ViewServiceProvider : Studio.Providers.ServiceProvider
{
    /// <summary>
    /// Register any application services.
    /// </summary>
    public void Register(IServiceCollection services)
    {
        services.AddRazorComponents()
            .AddInteractiveServerComponents();
    }
}

The Boot Method

The Boot method is called after all services have been registered and the application has been built. This method should be used to configure middleware, routing, and perform any setup that requires access to registered services. The app property provides access to the WebApplication instance:

namespace App.Providers;

public class ExceptionServiceProvider : Studio.Providers.ServiceProvider
{
    /// <summary>
    /// Bootstrap any application services.
    /// </summary>
    public void Boot()
    {
        if (!this.app.Environment.IsDevelopment())
        {
            this.app.UseExceptionHandler("/Error", createScopeForErrors: true);
            this.app.UseHsts();
        }
    }
}

Boot Method Dependency Injection

Dependencies may be added as parameters in the Boot method's signature. The service container will automatically try to inject any dependencies the Boot method needs:

using App.Factories;

public void Boot(ResponseFactory response)
{
    response.Make(() => 
    {
        // ...
    });
}

Configuring Providers

Sometimes, you may need to customize the behavior of a provider before it is initialized. This package makes this effortless by allowing you to pass a configuration callback when adding providers to your application.

When registering a provider, simply provide a closure as the second argument to the Add method. This closure will receive an instance of the provider, allowing you to set properties or invoke methods to tailor the provider's behavior to your application's specific needs.

For example, imagine you have a ViewServiceProvider that needs to know which Blazor component serves as your application's root. You can configure this directly during registration:

using Studio.Providers;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddProviders(providers =>
{
    providers
        .Add<AppServiceProvider>()
        .Add<AuthServiceProvider>()
        .Add<RouteServiceProvider>()
        .Add<ViewServiceProvider>(provider =>
        {
            provider.Component = typeof(App.Components.App);
        });
});

The callback is executed immediately when the provider is registered, giving you the opportunity to configure the provider instance before its Register and Boot methods are called. This pattern keeps your configuration declarative and centralized, making it easy to understand how each provider is customized for your application.

Registering Providers

Providers are registered in the Program.cs file using the AddProviders extension method. The order in which providers are registered determines the order in which they are executed. This is important when providers depend on services registered by other providers.

For example, if AuthServiceProvider registers authentication services that RouteServiceProvider needs during its boot phase, then AuthServiceProvider must be registered before RouteServiceProvider:

using Studio.Providers;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddProviders(providers =>
{
    providers
        .Add<AppServiceProvider>()
        .Add<AuthServiceProvider>()
        .Add<RouteServiceProvider>()
        .Add<ViewServiceProvider>();
});

In this example, AuthServiceProvider registers authentication services in its Register method. The RouteServiceProvider, which is registered after AuthServiceProvider, can safely use these authentication services in its Boot method to configure protected routes.

Executing Providers

After building the application, the UseProviders method must be called to execute the boot phase. This invokes the Boot method on all registered providers in the order they were registered:

var app = builder.Build();

app.UseProviders();

app.Run();

License

Studio.Providers is licensed under the MIT License. See the LICENSE file for details.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net9.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.0 114 1/21/2026
1.0.0 101 1/11/2026