Class IServiceProviderExtensions
- Namespace
- Songhay.Tests.Extensions
- Assembly
- SonghayCore.xUnit.dll
Extensions of IServiceProvider
public static class IServiceProviderExtensions
- Inheritance
-
IServiceProviderExtensions
- Inherited Members
Methods
GetRequiredServiceWithAssertion<TService>(IServiceProvider?)
Gets the required IServiceProvider service with the assertion that it is not null.
public static TService GetRequiredServiceWithAssertion<TService>(this IServiceProvider? serviceProvider) where TService : notnull
Parameters
serviceProvider
IServiceProviderThe service provider.
Returns
- TService
Type Parameters
TService
The type of the service.
Remarks
This member is convenient for returning global-state (non-scoped) services
like those registered with AddSingleton(IServiceCollection, Type, Type)
(e.g. Microsoft’s IConfiguration
contract).
This member also saves one line of code for the following scoped ServiceProvider
pattern:
using IServiceScope scope = _factory.Services.CreateScope();
IMyRepo myRepo = scope.ServiceProvider.GetRequiredService{IMyRepo}();
Assert.NotNull(myRepo);
where _factory
is an instance of an xUnit fixture
like IClassFixture{WebApplicationFactory{Program}}
.