Introduction
This article explains about the Configuration Source; i.e., creating simple services and injecting the services which we have created. Read previous parts of this article series using the below link before reading this article.
Definition
ASP.NET Core is a cross-platform and open-source framework. ASP.NET Core supports Windows, Mac or Linux operation systems. It is used to develop with modern, cloud-based and high-performance web applications.
Configuration Services
Configuration services are a method in ASP.NET core and it is in Startup.cs page. This method is called by runtime. Use this method to add the services to container.
It contains the IServiceCollection parameter in the method. It is used to inject the services in the pipeline. IserviceCollection namespace is “Microsoft.Extensions.DependencyInjection”.
Creating Simple Services
We are going to create simple services and inject these services using Configuration Services. Add the interface “IMessage.cs” then write the below code.
- namespace WebApplication15
- {
- public interface IMessages
- {
- string getValues();
- }
- class Message : IMessages
- {
- public string getValues()
- {
- return "Welcome To All";
- }
- }
- }
We have added the interface and class in the above code. The interface name is “IMessages” and class name is “Message”. IMessage interface is inherited to the Message class and writes the definition of getValues() method. This method returns the value as “Welcome To All”.
Injecting the Services
We have created simple services and we are going to inject the services using the ConfigurationServices. We are injecting the services using two techniques, one is “dependency Injection” and another one is “singleton design pattern”. Here we are going to see the small definition for both.
Dependency Injection is a design pattern. It is used to create dependent objects outside of the class and provides those objects to a class in different ways.
The singleton design pattern is a simple design pattern that restricts the instantiation of a class to one "single" instance. This is useful when exactly one object is needed to coordinate actions across the system.
Using the above two technique we are going to injecting and using the services. We are injecting service using below code.
- public void ConfigureServices(IServiceCollection services)
- {
- services.AddSingleton<IMessages, Message>();
- }
Adds a singleton service of the type specified in TService with an implementation type specified in TImplementation to the specified Microsoft.Extensions.DependencyInjection.IServiceCollection. Here Tservice is “IMessages” interface and TImplementation is “Message” class.
Call The Services
Configure method is used to configure the HTTP request pipeline. Here we are creating the reference for the IMessages interface. Using reference we can call the class method and get any values or process the values. We can find the code looks like below.
- public void Configure(IApplicationBuilder app, IHostingEnvironment env, IMessages messages) {
- if (env.IsDevelopment()) {
- app.UseDeveloperExceptionPage();
- }
-
- app.Run(async (context) => {
- var vals = messages.getValues();
- await context.Response.WriteAsync(vals);
- });
- }
Store the values into the variable from getVal() method. We have created the reference for the interface and are calling the method. We know now how the communication line is working, with an interface reference check which the singleton specified in TService (IMessages) with an implementation type specified in TImplementation (Message). We can get the expected output which looks like the below screenshot.
Hide the ConfigureServices
In the code, if we hide the ConfigureService method we will get an error.
- namespace WebApplication15
- {
- public class Startup
- {
- public Startup(IConfiguration config)
- {
-
- }
-
-
-
-
-
- public void Configure(IApplicationBuilder app, IHostingEnvironment env, IMessages messages)
- {
- if (env.IsDevelopment())
- {
- app.UseDeveloperExceptionPage();
- }
-
- app.Run(async (context) =>
- {
- var vals = messages.getValues();
- await context.Response.WriteAsync(vals);
- });
- }
- }
- }
We are getting the error as “No service for type 'WebApplication15.IMessages' has been registered”. In the below screenshot we can see the exact issue. Whenever we are creating new services we need to register or inject in the configuration services.
Conclusion
This article explained creating simple services and injecting the services using configuration services. I hope this really helps new learners, students, and freshers.