Agenda
In this article, we will learn about the in-built dependency injection IOC (Inversion of Control) container that comes out of the box with .net core development. This eliminates the use of external IOC Containers like AutoFac, Unity, etc.
Pre-requisite:
- .NET Core SDK
- Visual Studio Code
- Basic knowledge of IOC concept
The built-in container offers Constructor and method injection only. Property injection is not supported as of now, which can be achieved using third party containers like AutoFac, unity etc.
In-built container manages the classes and their implementations in the form of services which can be divided into two parts:
1. Framework provided These are the in-built services that can be injected without registering them in the container. They are by default registered in the container.
2. Application provided These are the user created services that can be injected after registering in the built-in container.
Lifetime of registered services: There are three types of registrations that are provided with IOC container of .net core. These registrations define the lifetime of the dependencies.
1. Transient: This will create a new instance every time it is used and accessed in the code.
2. Scoped: This will create a new instance for a single request and use the same object throughout that particular request. If a new request comes in, a new object will be assigned.
3. Singleton: This will create a new instance per lifecycle.
We will see all of them in action later in this article.
Extension methods: For all the three registration types mentioned above, .net core offers extension methods which makes it easy to register the objects in the container.
Enough information. Now, let’s see the demo.
Step 1: Install .NET Core 3.1 SDK from Microsoft’s official website.
Install the SDK from the downloaded installer.
Step 2: Install Visual Studio Code editor. Please go to https://code.visualstudio.com and download the installer based on your OS Type.
Step 3: We need to install an extension in VS Code which will activate C# features in VS code. So, install C# extension mentioned below.
Step 4: Create a folder at a location on your hard drive and open it in VS Code.
Step 5: Hit the following command in the integrated terminal to create a new .net core console application.
Command: dotnet new webapp
Step 6: Create a LogService that uses ILogger in-built service to log messages to the console. Create a Services folder -> LogService.cs. Copy the following code.
As you can see above, we injected ILogger to the LogService class and LogInformation method is used to Log the messages to the console at runtime.
Step 7: Let’s create another service named DemoService.cs file in the same folder and inject LogService using constructor injection.
In the above screenshot, we injected LogService in the constructor of this class and calls the LogMessage method with a hardcoded message.
Step 8: Now, we need to invoke DemoService when the application runs. So, let’s inject this service in Index.cshtml.cs file so that we can invoke this service on the Index page.
In the above code, we injected DemoService in the constructor and calling the Log method which should log the hardcoded message of previous step on the console.
Step 9: Now, if you have noticed, we have introduced two services LogService and DemoService which are being used as dependencies. So, we need to register them in the in-built container.
Open Startup.cs file and replace the following code.
In the above code, we registered two dependencies in the container services. Now our application container knows about the abstract and the concrete implementation of the newly created services.
So, when the Index page calls DemoService, DemoService will call LogService and finally LogService will call ILogger service to write the log messages to the console.
Step 10: Let’s run the application and see if we are able to see the message on the console.
Command: dotnet run
Now navigate to http://localhost:5000 and once we hit this URL, Index page will get hit and on the left side, you can see the hardcoded message along with Service name from which it is getting logged.
If you forget to register services in the container, you might get the below error. I commented DemoService declaration from Startup.cs and received the following error on the console.
So, be sure that ASP.NET developers register all those services which are being used as a dependency in any class file.