Class TestServiceProvider
- Namespace
- Bunit
- Assembly
- Bunit.Core.dll
Represents a IServiceProvider and IServiceCollection as a single type used for test purposes.
public sealed class TestServiceProvider : IServiceProvider, IServiceCollection, IList<ServiceDescriptor>, ICollection<ServiceDescriptor>, IEnumerable<ServiceDescriptor>, IEnumerable, IDisposable, IAsyncDisposable
- Inheritance
-
TestServiceProvider
- Implements
- Inherited Members
- Extension Methods
Constructors
TestServiceProvider(IServiceCollection?)
Initializes a new instance of the TestServiceProvider class
and sets its service collection to the provided initialServiceCollection, if any.
public TestServiceProvider(IServiceCollection? initialServiceCollection = null)
Parameters
initialServiceCollectionIServiceCollection
Properties
Count
public int Count { get; }
Property Value
IsProviderInitialized
Gets a value indicating whether this TestServiceProvider has been initialized, and
no longer will accept calls to the AddService's methods.
public bool IsProviderInitialized { get; }
Property Value
IsReadOnly
public bool IsReadOnly { get; }
Property Value
this[int]
public ServiceDescriptor this[int index] { get; set; }
Parameters
indexint
Property Value
Options
Gets or sets the ServiceProviderOptions used when the IServiceProvider is created.
public ServiceProviderOptions Options { get; set; }
Property Value
Methods
Add(ServiceDescriptor)
public void Add(ServiceDescriptor item)
Parameters
itemServiceDescriptor
AddFallbackServiceProvider(IServiceProvider)
Add a fall back service provider that provides services when the default returns null.
public void AddFallbackServiceProvider(IServiceProvider serviceProvider)
Parameters
serviceProviderIServiceProviderThe fallback service provider.
Clear()
public void Clear()
Contains(ServiceDescriptor)
public bool Contains(ServiceDescriptor item)
Parameters
itemServiceDescriptor
Returns
CopyTo(ServiceDescriptor[], int)
public void CopyTo(ServiceDescriptor[] array, int arrayIndex)
Parameters
arrayServiceDescriptor[]arrayIndexint
Dispose()
public void Dispose()
DisposeAsync()
public ValueTask DisposeAsync()
Returns
GetEnumerator()
public IEnumerator<ServiceDescriptor> GetEnumerator()
Returns
GetKeyedService(Type, object?)
Gets the service object of the specified type.
public object? GetKeyedService(Type serviceType, object? serviceKey)
Parameters
serviceTypeTypeAn object that specifies the type of service object to get.
serviceKeyobjectAn object that specifies the key of service object to get.
Returns
- object
A service object of type
serviceType.-or-
null if there is no service object of type
serviceType.
GetRequiredKeyedService(Type, object?)
Gets service of type serviceType from the IServiceProvider implementing
this interface.
public object GetRequiredKeyedService(Type serviceType, object? serviceKey)
Parameters
serviceTypeTypeAn object that specifies the type of service object to get.
serviceKeyobjectThe ServiceKey of the service.
Returns
- object
A service object of type
serviceType. Throws an exception if the IServiceProvider cannot create the object.
GetService(Type)
public object? GetService(Type serviceType)
Parameters
serviceTypeType
Returns
GetService<TService>()
Get service of type T from the test provider.
public TService? GetService<TService>()
Returns
- TService
A service object of type T or null if there is no such service.
Type Parameters
TServiceThe type of service object to get.
IndexOf(ServiceDescriptor)
public int IndexOf(ServiceDescriptor item)
Parameters
itemServiceDescriptor
Returns
Insert(int, ServiceDescriptor)
public void Insert(int index, ServiceDescriptor item)
Parameters
indexintitemServiceDescriptor
Remove(ServiceDescriptor)
public bool Remove(ServiceDescriptor item)
Parameters
itemServiceDescriptor
Returns
RemoveAt(int)
public void RemoveAt(int index)
Parameters
indexint
UseServiceProviderFactory(Func<IServiceCollection, IServiceProvider>)
Use a custom service provider factory for creating the underlying IServiceProvider.
public void UseServiceProviderFactory(Func<IServiceCollection, IServiceProvider> serviceProviderFactory)
Parameters
serviceProviderFactoryFunc<IServiceCollection, IServiceProvider>custom service provider factory
UseServiceProviderFactory<TContainerBuilder>(IServiceProviderFactory<TContainerBuilder>, Action<TContainerBuilder>?)
Use a custom service provider factory for creating the underlying IServiceProvider.
public void UseServiceProviderFactory<TContainerBuilder>(IServiceProviderFactory<TContainerBuilder> serviceProviderFactory, Action<TContainerBuilder>? configure = null) where TContainerBuilder : notnull
Parameters
serviceProviderFactoryIServiceProviderFactory<TContainerBuilder>custom service provider factory
configureAction<TContainerBuilder>builder configuration action
Type Parameters
TContainerBuilderType of the container builder. See IServiceProviderFactory<TContainerBuilder>