how to register Application Service with built-in IOC container and use it in our application.
- In order to let the IoC container automatically inject our application services, we first need to register them with an IOC container.
- Consider the following example of a simple ILog interface and its implementation class. We will see how to register it with a built-in IoC container and use it in our application.
public interface ILog {
void info(string str);
}
class MyConsoleLogger : ILog {
public void info(string str)
{
Console.WriteLine(str);
}
}
- ASP.NET Core allows us to register our application services with IoC container, in the ConfigureServices method of the Startup class. The ConfigureServices method includes a parameter of IServiceCollection type which is used to register application services.
- Let's register above ILog with IoC container in ConfigureServices() method asshown below.
- Example: Register Service
public class Startup {
public void ConfigureServices(IServiceCollection services) {
services.Add(new ServiceDescriptor(typeof(ILog),
new MyConsoleLogger()));
} // other code removed for clarity..
}
- In the above ex:
- Add() method of IServiceCollection instance is used to register a service with an IoC container.
- ServiceDescriptor is used to specify a service type and its instance. We have specified ILog as service type and MyConsoleLogger as its instance. This will register the ILog service as a singleton by default.
- Now, an IoC container will create a singleton object of MyConsoleLogger class and inject it in the constructor of classes wherever we include ILog as a constructor or method parameter throughout the application.
- Thus, we can register our custom application services with an IoC container in the ASP.NET Core application. There are other extension methods available for quick and easy registration of services.
Understanding Service Lifetime for Registered Service
- Built-in IoC container manages the lifetime of a registered service type. It automatically disposes of a service instance based on the specified lifetime.
- The built-in IoC container supports three kinds of lifetimes:
1. Singleton: IoC container will create and share a single instance of service throughout the application's lifetime.
2. Transient: The IoC container will create a new instance of the specified service type every time you ask for it.
3. Scoped: IoC container will create an instance of the specified service type once per request and will be shared in a single request.
The following example shows how to register a service with different lifetimes.
Example: Register a Service with Lifetime
public void ConfigureServices(IServiceCollection services)
{
// singleton
services.Add(new ServiceDescriptor(typeof(ILog), new MyConsoleLogger()));
services.Add(new ServiceDescriptor(typeof(ILog), typeof(MyConsoleLogger),
ServiceLifetime.Transient)); // Transient
services.Add(new serviceDescriptor(typeof(ILog), typeof(MyConsoleLogger),
ServiceLifetime.Scoped)); // Scoped
}
Comments
Post a Comment