Class ServiceProviderExtensions
- Namespace
- Codebelt.Extensions.Xunit.Hosting
- Assembly
- Codebelt.Extensions.Xunit.Hosting.dll
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
GetRequiredScopedService<T>(IServiceProvider)
Gets a required scoped service of type T from the IServiceProvider.
public static T GetRequiredScopedService<T>(this IServiceProvider provider)
Parameters
providerIServiceProviderThe IServiceProvider to retrieve the service from.
Returns
- T
The required service of type
T.
Type Parameters
TThe type of the service to retrieve.
Exceptions
- InvalidOperationException
There is no service of type
T.