Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Satya KarkiRank #78 · 24.6K reputationPosted Oct 24, 2022, 1:15 AM EDT
https://www.tutorialspoint.com/how-can-we-inject-the-service-dependency-into-the-controller-chash-asp-net-core
Rijwan AnsariRank #82 · 24.2K reputationPosted Oct 24, 2022, 1:13 AM EDT
You can do it following steps.
1. First create your interface and it's implementation.
public interface IDateTime {DateTime Now { get; } }public class SystemDateTime : IDateTime {public DateTime Now{get { return DateTime.Now; }} }2. Configure Service container
public void ConfigureServices(IServiceCollection services) {services.AddSingleton();services.AddControllersWithViews(); }3. Initiate into constructor of the controller where you want to call.
public class HomeController : Controller {private readonly IDateTime _dateTime;public HomeController(IDateTime dateTime){_dateTime = dateTime;}public IActionResult Index(){var serverTime = _dateTime.Now;if (serverTime.Hour < 12){ViewData["Message"] = "It's morning here - Good Morning!";}else if (serverTime.Hour < 17){ViewData["Message"] = "It's afternoon here - Good Afternoon!";}else{ViewData["Message"] = "It's evening here - Good Evening!";}return View();}That's it.
You can refer to Microsoft learn.
Uday DodiyaRank #239 · 10.3K reputationPosted Oct 6, 2022, 12:53 AM EDT
ASP.NET Core injects objects of dependency classes through constructor or method by using built-in IoC container.
The built-in container is represented by IServiceProvider implementation that supports constructor injection by default. The types (classes) managed by built-in IoC container are called services.
In order to let the IoC container automatically inject our application services, we first need to register them with IoC container.
Example
public interface ILog{ void info(string str); } class MyConsoleLogger : ILog{ public void info(string str){ Console.WriteLine(str); } }ASP.NET Core allows us to register our application services with IoC container, in the ConfigureServices method of the Startup class. The ConfigureServices method includes a parameter of IServiceCollection type which is used to register application services
Register ILog with IoC container in ConfigureServices() method as shown below.
public class Startup{ public void ConfigureServices(IServiceCollection services){ services.AddSingleton(); } }Add() method of IServiceCollection instance is used to register a service with an IoC container
We have specified ILog as service type and MyConsoleLogger as its instance This will register ILog service as a singleton Now, an IoC container will create a singleton object of MyConsoleLogger class and inject it in the constructor of classes wherever we include ILog as a constructor or method parameter throughout the application.
Aravind GovindarajRank #430 · 5.8K reputationPosted Oct 23, 2022, 1:37 PM EDT
Hi Naresh,
You can use it here in different flavors, It's based on how you would like to set it up. Here we go the options are
To inject, you have to register in the startup class which eventually binds objects using the Inversion of Control principle.
Sandhiya PriyaRank #26 · 43.2K reputationPosted Jan 2, 2026, 6:56 AM EST
You can inject a service dependency into a controller using Dependency Injection (DI).
In ASP.NET / ASP.NET Core, this is done through constructor injection, which is the recommended and most common approach.
Step 1: Create a service interface
public interface IProductService { List GetProducts(); }Step 2: Implement the service
public class ProductService : IProductService { public List GetProducts() { return new List { "Laptop", "Mobile", "Tablet" }; } }Step 3: Register the service in the DI container
ASP.NET Core (.NET 6 / .NET 7 / .NET 8)
(You can also use
AddSingletonorAddTransientbased on requirement.)Step 4: Inject the service into the controller
public class ProductController : Controller { private readonly IProductService _productService; public ProductController(IProductService productService) { _productService = productService; } public IActionResult Index() { var products = _productService.GetProducts(); return View(products); } }How it works
ASP.NET Core automatically creates the service object
Injects it through the controller constructor
Manages the service lifetime
Types of Dependency Injection
Constructor Injection (Recommended)
Property Injection
Method Injection
interview answer
Service dependency is injected into a controller by registering the service in the DI container and using constructor injection in the controller.
If you want, I can also explain:
Scoped vs Transient vs SingletonDI in Web API vs MVC
Unit testing with DI
Darshan AdakaneRank #344 · 7.3K reputationPosted Jan 6, 2026, 5:46 AM EST
Dependency Injection means giving a controller what it needs, instead of the controller creating it by itself. Let see how can do it using most recommended and simplest approach -Constructor Injection.
Step 1: Create a Service Interface
Step 2: Create the Service Implementation
Step 3: Register the Service (Program.cs)
In Progam.cs file, add this :
Step 4: Inject the Service into the Controller
What Just Happened?
Controller says:
“I need an
IEmailService”.NET looks at registrations
Finds
EmailServiceCreates it
Passes it into the controller
You never used
new EmailService()— that’s the key.Hope this helps.