ServiceProviderValidationExtensions.Hosting
0.1.1
dotnet add package ServiceProviderValidationExtensions.Hosting --version 0.1.1
NuGet\Install-Package ServiceProviderValidationExtensions.Hosting -Version 0.1.1
<PackageReference Include="ServiceProviderValidationExtensions.Hosting" Version="0.1.1" />
paket add ServiceProviderValidationExtensions.Hosting --version 0.1.1
#r "nuget: ServiceProviderValidationExtensions.Hosting, 0.1.1"
// 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 | Versions 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. |
-
net7.0
- Microsoft.Extensions.Hosting (>= 7.0.0)
- ServiceProviderValidationExtensions (>= 0.1.1)
-
net8.0
- Microsoft.Extensions.Hosting (>= 7.0.0)
- ServiceProviderValidationExtensions (>= 0.1.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.