IServiceProvider provider = GetServiceProvider(); MyService service = provider.GetService(); service.DoSomething();
IServiceCollection services = new ServiceCollection(); services.AddSingletonThis example registers an instance of MyService with the service collection and then builds an IServiceProvider that can be used to retrieve the service. Package/Library: Microsoft.Extensions.DependencyInjection(); IServiceProvider provider = services.BuildServiceProvider();