using Microsoft.Extensions.DependencyInjection;
public static void Main()
using (var rootServiceProvider = BuildServices())
using (var rootScope = rootServiceProvider.CreateScope())
var provider = rootScope.ServiceProvider;
var genericStr1 = provider.GetRequiredService<IGenericTest<string>>();
var genericStr2 = provider.GetRequiredService<IGenericTest<string>>();
var genericInt = provider.GetRequiredService<IGenericTest<int>>();
genericStr1.Value = "test";
Console.WriteLine(genericStr2.Value);
Console.WriteLine(genericInt.Value.ToString());
public static ServiceProvider BuildServices(Action<IServiceCollection> configure = null) {
var services = new ServiceCollection();
services.AddSingleton(typeof(IGenericTest<>), typeof(GenericTest<>));
return services.BuildServiceProvider();
public interface IGenericTest<T>
public class GenericTest<T> : IGenericTest<T>
public T Value {get; set; }