Dependency Injection for .Net Core console application
As many organizations are now migrating their on-premise applications to the cloud platform now there are many requirements to convert legacy .Net application written in .Net Fullframework to .Net Core.
In this article, I want to share a simple way to enable the .Net core dependency injection, especially on a console application. If you create a new Asp .Net Web API or Web App the project scaffolding template will provide you the initial code for dependency injection. But console applications you will need to do as below.
First I will start by creating a new project.
I use the visual code terminal to create a new project called "sample-console-app" so the project name will be constructing from the directory name where you execute the below command.
> dotnet new console
The above command will create you the project and you will see your project view like below.
> dotnet restore
> dotnet run
Now you can see that it prints the "Hello world!" on the console view.
At this point, we confirmed that our base project is running as expected. Now we need to introduce a new class library which contains the business logic or services.
So, I navigate to my project root folder and create a new folder for my "sample-class-lib" project.
> cd ..
> mkdir sample-class-lib
> dotnet new classlib
Once you execute the above command you will see another project in your solution explorer.
So now we need to reference this new class library project into our main project.
You can add project reference in .Net Core with the below command
> dotnet add reference ../sample-class-lib/sample-class-lib.csproj
Once the above command is executed you can see in your main project's .csproj file you will see the new reference you added.
Just run the build command again and see if it builds both projects or not. Once the above steps are successfully completed, you can introduce your service or business logic classes and interfaces to the sample-class-lib project. Here is my sample application, I introduced ICalculator and ICalculatorService interfaces and Calculator and CalculatorService concrete classes. I introduced a simple sum calculation method into my service so I will be using that in my application.
Now my class library is completed so I'm going to modify the console application code to inject the service and call the Sum() method.
Before doing that it's better to mention the extension library that .Net Core is using for dependency injection. Microsoft.Extensions.DependancyInjection is the package you need to install on your main project in order to enable DI on your startup project.
You can install Microsoft.Extensions.DependancyInjection nuget by executing the below command.
> dotnet add package Microsoft.Extensions.DependancyInjection
This command will add the latest stable package to your project.
Now you have all the dependencies installed and just modify the Program.cs class as below to wire the DI framework in your project.
Everything is completed at this stage. Now run the application and see if you get the result printed on your console.
These are the minimum changes you need if you want to enable dependency injection on your dotnet core console application.
You can get the full source code from this git repository.
Happy coding.
Cheers!!
Comments
Post a Comment