ASP.NET Core: Getting Started (Part 5)
Introduction
This article explains about the Configuration Source, Creating simple services and injecting the services which we have created. Reading the ASP.NET Core previous part of these articles using the below link before reading this article.
- ASP.NET Core: Getting Started (Part 1)
- ASP.NET Core: Getting Started (Part 2)
- ASP.NET Core: Getting Started (Part 3)
- ASP.NET Core: Getting Started (Part 4)
Definition
ASP.NET Core is a cross-platform and open-source framework. ASP.NET Core supports Windows, Mac or Linux operation system. It is used to developing 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 call by runtime. Use this method 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 injecting 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 inherited to the Message class and written 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 injecting 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 calling the method. We know now how communication line is working, 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 below screenshot.
Hide the ConfigureServices
In the code, if we hide the ConfigureService method we will get the error.
namespace WebApplication15
{
public class Startup
{
public Startup(IConfiguration config)
{
}
//public void ConfigureServices(IServiceCollection services)
//{
// services.AddSingleton<IMessages, Message>();
//}
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, if command the configuration services. Whenever creating new services we need to register or inject in the configuration services
Conclusion
This article explained the Creating simple services and injecting the services using configuration services. I hope this really helps to new learners, students, and freshers.