Getting Started With ASP.NET Core - Part Five

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.
  1. namespace WebApplication15  
  2. {  
  3.     public interface IMessages  
  4.     {  
  5.         string getValues();  
  6.     }  
  7.     class Message : IMessages  
  8.     {  
  9.         public string getValues()  
  10.         {  
  11.             return "Welcome To All";  
  12.         }  
  13.     }  

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.
  1. public void ConfigureServices(IServiceCollection services)  
  2. {  
  3.     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.
  1. public void Configure(IApplicationBuilder app, IHostingEnvironment env, IMessages messages) {  
  2.  if (env.IsDevelopment()) {  
  3.   app.UseDeveloperExceptionPage();  
  4.  }  
  5.   
  6.  app.Run(async (context) => {  
  7.   var vals = messages.getValues();  
  8.   await context.Response.WriteAsync(vals);  
  9.  });  

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.
  1. namespace WebApplication15  
  2. {  
  3.     public class Startup  
  4.     {  
  5.         public Startup(IConfiguration config)  
  6.         {  
  7.              
  8.         }  
  9.         //public void ConfigureServices(IServiceCollection services)  
  10.         //{  
  11.         //    services.AddSingleton<IMessages, Message>();  
  12.         //}  
  13.   
  14.         public void Configure(IApplicationBuilder app, IHostingEnvironment env, IMessages messages)  
  15.         {  
  16.             if (env.IsDevelopment())  
  17.             {  
  18.                 app.UseDeveloperExceptionPage();  
  19.             }  
  20.   
  21.             app.Run(async (context) =>  
  22.             {  
  23.                 var vals = messages.getValues();  
  24.                 await context.Response.WriteAsync(vals);  
  25.             });  
  26.         }  
  27.     }  
  28. }
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.