Skip to content

Instantly share code, notes, and snippets.

@hikalkan
Created June 28, 2019 14:25
Show Gist options
  • Save hikalkan/c02dcafa4137301bd4f58d9e8d7e3217 to your computer and use it in GitHub Desktop.
Save hikalkan/c02dcafa4137301bd4f58d9e8d7e3217 to your computer and use it in GitHub Desktop.
Some tests shows how to use scoped services from singleton services
using System;
using Microsoft.Extensions.DependencyInjection;
using Shouldly;
using Xunit;
namespace MsDependencyInjectionTests
{
public class SingletonsDependingScoped
{
[Fact]
public void Singletons_Can_Not_Inject_Scoped()
{
var serviceScopeFactory = GetServiceScopeFactory();
using (var scope = serviceScopeFactory.CreateScope())
{
Assert.ThrowsAny<InvalidOperationException>(() =>
{
scope.ServiceProvider
.GetRequiredService<MySingletonServiceInjectsScoped>()
.ShouldNotBeNull();
});
}
}
[Fact]
public void Singletons_Can_Resolve_Scoped_In_A_Scope_Created_From_IServiceScopeFactory()
{
var serviceScopeFactory = GetServiceScopeFactory();
using (var scope = serviceScopeFactory.CreateScope())
{
scope.ServiceProvider
.GetRequiredService<MySingletonServiceResolvesScopedUsingServiceScopeFactory>()
.ShouldNotBeNull();
}
}
[Fact]
public void Singletons_Can_Resolve_Scoped_In_A_Scope_Created_From_IServiceProvider()
{
var serviceScopeFactory = GetServiceScopeFactory();
using (var scope = serviceScopeFactory.CreateScope())
{
scope.ServiceProvider
.GetRequiredService<MySingletonServiceResolvesScopedUsingServiceProvider>()
.ShouldNotBeNull();
}
}
private static IServiceScopeFactory GetServiceScopeFactory()
{
var services = new ServiceCollection();
services
.AddSingleton<MySingletonServiceInjectsScoped>()
.AddSingleton<MySingletonServiceResolvesScopedUsingServiceScopeFactory>()
.AddSingleton<MySingletonServiceResolvesScopedUsingServiceProvider>()
.AddScoped<MyScopedService>();
return services
.BuildServiceProvider(
new ServiceProviderOptions
{
ValidateScopes = true
})
.GetRequiredService<IServiceScopeFactory>();
}
public class MySingletonServiceInjectsScoped
{
public MySingletonServiceInjectsScoped(MyScopedService scopedService)
{
}
}
public class MySingletonServiceResolvesScopedUsingServiceScopeFactory
{
private readonly IServiceScopeFactory _serviceScopeFactory;
public MySingletonServiceResolvesScopedUsingServiceScopeFactory(IServiceScopeFactory serviceScopeFactory)
{
_serviceScopeFactory = serviceScopeFactory;
using (var scope = _serviceScopeFactory.CreateScope())
{
scope.ServiceProvider.GetRequiredService<MyScopedService>();
}
}
}
public class MySingletonServiceResolvesScopedUsingServiceProvider
{
private readonly IServiceProvider _serviceProvider;
public MySingletonServiceResolvesScopedUsingServiceProvider(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
using (var scope = _serviceProvider.CreateScope())
{
scope.ServiceProvider.GetRequiredService<MyScopedService>();
}
}
}
public class MyScopedService
{
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment