Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What framework uses `IServiceProvider`?

Is the IServiceProvider basically just a generic interface for any IOC container, or is it used for a specific framework? I'm rolling my own light weight IOC container and I am wanting to know if I should implement it. Are there any other interfaces that I should implement? I'm not really interested in either MEF or Unity. I've used both extensively and they don't really work for my current project.

like image 717
Jordan Avatar asked Feb 04 '13 20:02

Jordan


People also ask

What is IServiceProvider in .NET Core?

This interface is implemented by a class or value type that provides a service to other objects. The GetService method of this interface obtains the object that provides the service. The IServiceProvider interface is implemented by a number of types, including System. Web. HttpContext, System.

How does IServiceProvider work?

The IServiceProvider is responsible for resolving instances of types at runtime, as required by the application. These instances can be injected into other services resolved from the same dependency injection container. The ServiceProvider ensures that resolved services live for the expected lifetime.

Can I inject IServiceProvider?

You can inject an instance of type IServiceProvider into any method of a class. You can also take advantage of the ApplicationServices property of the IApplicationBuilder interface and the RequestServices property of the HttpContext class to retrieve an IServiceProvider instance.

What is IServiceCollection in .NET Core?

} IServiceCollection is the collection of the service descriptors. We can register our services in this collection with different lifestyles (Transient, scoped, singleton) IServiceProvider is the simple built-in container that is included in ASP.NET Core that supports constructor injection by default.


1 Answers

IServiceProvider is an imported (or perhaps held-over) COM interface that is intended to be used for private features in the context of the object whom you interrogate for a Service. The term 'Service' is applied rather loosely here, it originally meant any COM object that could be returned based upon what GUID is given.

IServiceProvider @ MSDN (.NET reference)
IServiceProviderImpl Class @ MSDN (C++ ATL reference)

In .NET, you don't need to implement it unless you have a client that specifically supports it, and in many cases you won't need to add yet another level of indirection that is implied by using IServiceProvider. Also, you can devise your own scheme to share common objects or implement other use patterns based upon IoC / Dependency Injection that are more flexible or more rigid as dictated by your needs.

One good historical context for IServiceProvider is the IE Browser Plugin Spec. Here, it is used to allow plugin components to use Browser Host features in-context. In a COM context, this interface is useful because it hides the details of instantiation and also can be used as part of a object construction and utilization strategy to avoid reference loops.

WebBrowser Customization (Part 2) @ MSDN

like image 77
meklarian Avatar answered Oct 15 '22 10:10

meklarian