How to get an instance of IServiceProvider in .NET Core?

后端 未结 6 1708
不知归路
不知归路 2020-12-08 00:03

IServiceProvider is an interface with single method:

object GetService(Type serviceType);

It\'s used to create instances of ty

6条回答
  •  清歌不尽
    2020-12-08 00:54

    This is the default implementation of IServiceCollection from Microsoft: https://github.com/aspnet/DependencyInjection/blob/master/src/DI/ServiceCollection.cs

    Looking at the code then you should be able to get an IServiceCollection simply by calling:

    var serviceCollection = new Microsoft.Extensions.DependencyInjection.ServiceCollection();
    

    Hope that helps :)

提交回复
热议问题