Explain important interfaces and classes for built-in loc containers.
The followings are important interfaces and classes for built-in loc containers:
Interfaces
1. IService Provider
2. IServiceCollection
Classes
1. Service Provider
2. ServiceCollection.
3. Service Description
4. ServiceCollection Service Extensions
5. ServiceCollection ContainerBuilderExtensions
Figure:- Interfaces and classes for built-in loc containers
Interfaces
1. IService Provider
2. IServiceCollection
1. IServiceCollection
• We can register application services with a built-in loC container in the Configure method of Startup class by using IService Collection. IServiceCollection interface is an empty interface. It just inherits IList<servicedescriptor>.
• The ServiceCollection class implements the IService Collection interface.
• So, the services you add in the IServiceCollection type instance, actually create an instance of Service Descriptor and add it to the list.
2. IService Provider
• IService Provider includes the GetService method.
• The ServiceProvider class implements the IService Provider interface which returns registered services with the container.
• We cannot instantiate the Service Provider class because its constructors are marked with internal access modifiers.
Classes
1. Service Provider
2. ServiceCollection.
3. Service Description
4. ServiceCollection Service Extensions
5. ServiceCollection ContainerBuilderExtensions
1. ServiceCollection Service Extensions
• The ServiceCollection Service Extensions class includes extension methods related to service registrations which can be used to add services with a lifetime. AddSingleton, AddTransient, AddScoped extension methods defined in this class.
2. ServiceCollection Container Builder Extensions
• Service Collection Container Builder Extensions class includes Build Service Provider extension method which creates and returns an instance of Service Provider.
• There are three ways to get an instance of IService Provider:
a)Using IApplication Builder
b) Using HttpContext
c) Using IServiceCollection
a)Using IApplication Builder
We can get the services in Configure method using ApplicationBuilder's Application Services property as shown below.
public void Configure(IService Provider pro, IApplicationBuilder app, IHosting Environment env)
{
var services = app. ApplicationServices;
var logger services.GetService<ILog>() }
//other code removed for clarity.
}
b) Using HttpContext
var services HttpContext.Request Services;
var log (ILog) services.GetService (typeof (ILog));
c) Using IServiceCollection
public void ConfigureServices (IService Collection services)
{
var serviceProvider = services. Build Service Provider();
}
Comments
Post a Comment