Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access IServiceCollection and/or IServiceProvider outside of Startup class?

I am using Microsoft.Extensions.DependencyInjection in .Net Farmework 4.6.2 class libraries. How to access IServiceCollection and/or IServiceProvider from outside the source code where they are instantiated, Main(), for instance? I could create a static class exposing this property in a separate class library and referenced it but I am wondering if there is any other better way of achieving this objective. .Net Framework has ServiceLocator.Current.GetInstance. Is there any similar thing in Microsoft.Extensions.DependencyInjection? Any advice and insight is appreciated.

like image 906
Kok How Teh Avatar asked Apr 20 '18 08:04

Kok How Teh


People also ask

How do I get an instance of IServiceProvider?

An instance of IServiceProvider itself can be obtained by calling a BuildServiceProvider method of an IServiceCollection. IServiceCollection is a parameter of ConfigureServices method in a Startup class. It seems to be magically called with an instance of IServiceCollection by the framework.

Can I access a database during startup in asp net core?

Yes, you can access the database!

How do you resolve IServiceCollection?

Resolve dependencies using IServiceProvider Once the container has been created, the IServiceCollection instance is composed into an IServiceProvider instance. You can use this instance to resolve services. You can inject an instance of type IServiceProvider into any method of a class.

What is the difference between ServiceProvider and iservicecollection?

So, the services you add in the IServiceCollection type instance, it actually creates an instance of ServiceDescriptor and adds it to the list. IServiceProvider includes GetService method. The ServiceProvider class implements IServiceProvider interface which returns registered services with the container.

How to get an instance of iserviceprovider in ASP NET Core?

There are three ways to get an instance of IServiceProvider : We can get the services in Configure method using IApplicationBuilder's ApplicationServices property as shown below. Want to check how much you know ASP.NET Core?

How do I add a class to an iservicecollection?

To do that, you use one of three methods available from the IServiceCollection class: AddScoped, AddTransient and AddSingleton. The syntax for all three is identical so, assuming you have a class called ShippingCalculator that implements the IShipCalc interface, you'd use code like this to add the class to the collection:

How to get an instance of ServiceProvider in servicecollectioncontainerbuilderextensions?

ServiceCollectionContainerBuilderExtensions class includes BuildServiceProvider extension method which creates and returns an instance of ServiceProvider . There are three ways to get an instance of IServiceProvider : We can get the services in Configure method using IApplicationBuilder's ApplicationServices property as shown below.


1 Answers

Different from the IServiceProvider, the IServiceCollection can't be injected into a class constructor.

As many have already said, you should avoid using them directly. But if you really need to do so for the IServiceCollection, you can create a "Provider", such as:

    public interface IServiceCollectionProvider 
    {
        IServiceCollection ServiceCollection { get; }
    }

    public sealed class ServiceCollectionProvider: IServiceCollectionProvider
    {
        public ServiceCollectionProvider(IServiceCollection serviceCollection)
        {
            ServiceCollection = serviceCollection;
        }

        public IServiceCollection ServiceCollection { get; }
    }

Registering:

services.AddSingleton<IServiceCollectionProvider>(new ServiceCollectionProvider(services));

Using:

    public class YourController : Controller
    {
        private readonly IServiceProvider _provider;
        private readonly IServiceCollection _services;

        public YourController (IServiceProvider provider, IServiceCollectionProvider serviceCollectionProvider)
        {
            _provider = provider;
            _services = serviceCollectionProvider.ServiceCollection;
        }
    }
like image 200
GuiBGP Avatar answered Nov 17 '22 01:11

GuiBGP