How to use TinyIoC in ASP.NET Core
By FoxLearn 1/6/2025 9:08:02 AM 23
In ASP.NET Core applications, you may need a simple, lightweight IoC container to avoid the complexity and overhead of more resource-intensive solutions, especially when a full-featured container isn't necessary.
Inversion of Control & Dependency Injection
Inversion of Control (IoC) and Dependency Injection (DI) are architectural patterns that improve the modularity, testability, and maintainability of applications. IoC is a design principle that decouples components, allowing for easier replacement of dependencies and better support for testing. DI is a specific approach to implementing IoC, where dependencies are injected into a class rather than created within it. Beyond DI, there are other IoC implementation techniques, including events, delegates, template patterns, factory methods, and service locators.
To install TinyIoC in your Web API project, open Visual Studio and right-click on the project in Solution Explorer. Select "Manage NuGet Packages," search for TinyIoC, and install it.
Alternatively, you can use the NuGet Package Manager console with the command:
PM> Install-Package TinyIoC
In this case, we'll create a LoggingService
that logs messages to the console, and we’ll use it in an API controller to show how to set up and resolve dependencies using TinyIoC.
First, define a LoggingService
class and an interface ILoggerService
that declares the logging method.
using System; namespace TinyIoC_Demo { public class LoggingService : ILoggerService { public void Log(string message) { Console.WriteLine($"Log message: {message}"); } } }
The ILoggerService
interface:
namespace TinyIoC_Demo { public interface ILoggerService { void Log(string message); } }
Configure TinyIoC Container in ASP.NET Core
In your Program.cs
file, register the ILoggerService
and LoggingService
with the TinyIoC container.
TinyIoCContainer.Current.Register<ILoggerService, LoggingService>().AsSingleton();
If you have multiple dependencies to register, you can create a Bootstrap
class to keep the code clean:
internal static class Bootstrap { internal static void RegisterDependencies() { TinyIoCContainer.Current.Register<ILoggerService, LoggingService>().AsSingleton(); // Register other services here if needed } }
Now, call RegisterDependencies
in your Program.cs
to register all your services:
Bootstrap.RegisterDependencies();
Here's the complete code for Program.cs
that registers the dependencies and configures the application:
using TinyIoC; using TinyIoC_Demo; var builder = WebApplication.CreateBuilder(args); // Register dependencies Bootstrap.RegisterDependencies(); // Add services to the container builder.Services.AddControllers(); var app = builder.Build(); // Configure the HTTP request pipeline app.UseAuthorization(); app.MapControllers(); app.Run();
Resolve Dependencies in the Controller
Now, let’s create a new API controller and use the ILoggerService
to log messages. The LoggingService
will be resolved using TinyIoC's Resolve
method.
Create a new API controller and use ILoggerService
in the LogController
:
using Microsoft.AspNetCore.Mvc; using TinyIoC; namespace TinyIoC_Demo.Controllers { [Route("api/[controller]")] [ApiController] public class LogController : ControllerBase { private readonly ILoggerService _loggerService; public LogController() { // Resolving the dependency from the TinyIoC container _loggerService = TinyIoCContainer.Current.Resolve<ILoggerService>(); } [HttpGet] public ActionResult Get() { _loggerService.Log("This is a test log message."); return Ok("Log message written to console."); } } }
Now, when you run your ASP.NET Core application and navigate to the endpoint api/log
, it will log the message "This is a test log message."
to the console, demonstrating how the LoggingService
is resolved and used in the controller.