Table of Contents

ServiceProviderExtensions Class

Definition

Namespace
Codebelt.Extensions.Xunit.Hosting
Assemblies
Codebelt.Extensions.Xunit.Hosting.dll
Source
src/Codebelt.Extensions.Xunit.Hosting/ServiceProviderExtensions.cs

Provides extension methods for the IServiceProvider interface.

public static class ServiceProviderExtensions
Inheritance
ServiceProviderExtensions

Examples

The following example retrieves a scoped service of type T from an IServiceProvider by calling GetRequiredScopedService<T>. The extension creates a new scope, resolves the service from the scope's provider, and disposes the scope before returning.

using System;
using Codebelt.Extensions.Xunit.Hosting;
using Microsoft.Extensions.DependencyInjection;

namespace HostFixtureTests;

public class ServiceResolver
{
    public T Resolve<T>(IServiceProvider provider) where T : notnull
    {
        return provider.GetRequiredScopedService<T>();
    }
}

Methods

Name Description
GetRequiredScopedService<T>(IServiceProvider)

Gets a required scoped service of type T from the IServiceProvider.