Table of Contents

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

initialServiceCollection IServiceCollection

Properties

Count

public int Count { get; }

Property Value

int

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

bool

IsReadOnly

public bool IsReadOnly { get; }

Property Value

bool

this[int]

public ServiceDescriptor this[int index] { get; set; }

Parameters

index int

Property Value

ServiceDescriptor

Options

Gets or sets the ServiceProviderOptions used when the IServiceProvider is created.

public ServiceProviderOptions Options { get; set; }

Property Value

ServiceProviderOptions

Methods

Add(ServiceDescriptor)

public void Add(ServiceDescriptor item)

Parameters

item ServiceDescriptor

AddFallbackServiceProvider(IServiceProvider)

Add a fall back service provider that provides services when the default returns null.

public void AddFallbackServiceProvider(IServiceProvider serviceProvider)

Parameters

serviceProvider IServiceProvider

The fallback service provider.

Clear()

public void Clear()

Contains(ServiceDescriptor)

public bool Contains(ServiceDescriptor item)

Parameters

item ServiceDescriptor

Returns

bool

CopyTo(ServiceDescriptor[], int)

public void CopyTo(ServiceDescriptor[] array, int arrayIndex)

Parameters

array ServiceDescriptor[]
arrayIndex int

Dispose()

public void Dispose()

DisposeAsync()

public ValueTask DisposeAsync()

Returns

ValueTask

GetEnumerator()

public IEnumerator<ServiceDescriptor> GetEnumerator()

Returns

IEnumerator<ServiceDescriptor>

GetKeyedService(Type, object?)

public object? GetKeyedService(Type serviceType, object? serviceKey)

Parameters

serviceType Type
serviceKey object

Returns

object

GetRequiredKeyedService(Type, object?)

public object GetRequiredKeyedService(Type serviceType, object? serviceKey)

Parameters

serviceType Type
serviceKey object

Returns

object

GetService(Type)

public object? GetService(Type serviceType)

Parameters

serviceType Type

Returns

object

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

TService

The type of service object to get.

IndexOf(ServiceDescriptor)

public int IndexOf(ServiceDescriptor item)

Parameters

item ServiceDescriptor

Returns

int

Insert(int, ServiceDescriptor)

public void Insert(int index, ServiceDescriptor item)

Parameters

index int
item ServiceDescriptor

Remove(ServiceDescriptor)

public bool Remove(ServiceDescriptor item)

Parameters

item ServiceDescriptor

Returns

bool

RemoveAt(int)

public void RemoveAt(int index)

Parameters

index int

UseServiceProviderFactory(Func<IServiceCollection, IServiceProvider>)

Use a custom service provider factory for creating the underlying IServiceProvider.

public void UseServiceProviderFactory(Func<IServiceCollection, IServiceProvider> serviceProviderFactory)

Parameters

serviceProviderFactory Func<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

serviceProviderFactory IServiceProviderFactory<TContainerBuilder>

custom service provider factory

configure Action<TContainerBuilder>

builder configuration action

Type Parameters

TContainerBuilder

Type of the container builder. See IServiceProviderFactory<TContainerBuilder>

Progress Telerik

Premium sponsor: Progress Telerik.

Packt

Editorial support provided by Packt.

.NET Foundation

Supported by the .NET Foundation.