ServiceProviderValidationExtensions.Hosting 0.1.1

dotnet add package ServiceProviderValidationExtensions.Hosting --version 0.1.1                
NuGet\Install-Package ServiceProviderValidationExtensions.Hosting -Version 0.1.1                
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="ServiceProviderValidationExtensions.Hosting" Version="0.1.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ServiceProviderValidationExtensions.Hosting --version 0.1.1                
#r "nuget: ServiceProviderValidationExtensions.Hosting, 0.1.1"                
#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.
// Install ServiceProviderValidationExtensions.Hosting as a Cake Addin
#addin nuget:?package=ServiceProviderValidationExtensions.Hosting&version=0.1.1

// Install ServiceProviderValidationExtensions.Hosting as a Cake Tool
#tool nuget:?package=ServiceProviderValidationExtensions.Hosting&version=0.1.1                

ServiceProviderValidationExtensions

This library makes more fine-grained control possible for registration in the ServiceProvider. These are especially useful for large complicated application.

It contains the following features:

Exclusive service registration

A service can be registered as exclusive, meaning that not more than once occourence of it can exists in the ServiceProvider. To register a service as exclusive, call SetExclusiveService on the IServiceCollection:

serviceCollection.SetExclusiveService<IMyService>()

Alternatively, overloads for the more common signatures of AddSingleton, AddScoped and AddTransient exists, e.g.:

serviceCollection.AddSingleton<MyService>(ServiceValidation.ExclusiveService);
serviceCollection.AddSingleton<IMyService, MyService>(ServiceValidation.ExclusiveService);

Exclusive implementation registration

An implementation can be registered as exclusive, meaning that not more than once occourence of the combination of the type of service and the implementation can exists in the ServiceProvider. To register a service as exclusive, call SetExclusiveImplementation on the IServiceCollection:

serviceCollection.SetExclusiveImplementation<IMyService, MyService>()

Alternatively, overloads for the more common signatures of AddSingleton, AddScoped and AddTransient exists, e.g.:

serviceCollection.AddSingleton<IMyService, MyService>(ServiceValidation.ExclusiveService);

Reporting

The reporting functionality makes it possible to get insights into the registrations in the ServiceProvider. It currently supports detecting duplicate service registrations.

void ConfigureReporting(ReportConfigurer rb)
{
    rb.OnDuplicateService(
        ds => Console.WriteLine($"{ds.ServiceType} is registered {dsc.ImplementationTypes.Count} times"),
        c => c.Except(typeof(IConfigureOptions<>)).Except<ILoggerProvider>());
}

Host.CreateDefaultBuilder().UseServiceProviderExtendedValidation(ConfigureReporting).Build();

Invoking the validation

There are two ways of invoking the validation, as described below. In case any validation fails, a ServiceProviderValidationException is thrown.

Hosting extensions

Reference the ServiceProviderValidationExtensions.Hosting package and use the UseServiceProviderExtendedValidation extension method.

Host.CreateDefaultBuilder()
    .UseServiceProviderExtendedValidation()

or

Host.CreateApplicationBuilder()
    .ConfigureContainerWithServiceProviderExtendedValidation()

Manually building the ServiceProvider

Call BuildServiceProviderWithValidation on the IServiceCollection, e.g.

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

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
0.1.1 154 3/3/2024
0.1.0 137 3/2/2024