How to Register a Service in C#: Simple Guide
In C#, you register a service by adding it to the
IServiceCollection using methods like AddSingleton, AddScoped, or AddTransient. This is typically done in the ConfigureServices method of your application to enable dependency injection.Syntax
To register a service in C#, use the IServiceCollection interface with one of these methods:
AddSingleton<TService, TImplementation>(): Creates one instance for the app lifetime.AddScoped<TService, TImplementation>(): Creates one instance per client request.AddTransient<TService, TImplementation>(): Creates a new instance every time requested.
Each method takes the service interface and the class that implements it.
csharp
services.AddSingleton<IService, ServiceImplementation>(); services.AddScoped<IService, ServiceImplementation>(); services.AddTransient<IService, ServiceImplementation>();
Example
This example shows how to register and use a simple service in a console app using dependency injection.
csharp
using System; using Microsoft.Extensions.DependencyInjection; public interface IMessageService { void SendMessage(string message); } public class ConsoleMessageService : IMessageService { public void SendMessage(string message) { Console.WriteLine($"Message: {message}"); } } class Program { static void Main() { var services = new ServiceCollection(); services.AddSingleton<IMessageService, ConsoleMessageService>(); var provider = services.BuildServiceProvider(); var messageService = provider.GetService<IMessageService>(); messageService.SendMessage("Hello, service registration!"); } }
Output
Message: Hello, service registration!
Common Pitfalls
Common mistakes when registering services include:
- Registering the implementation type instead of the interface, which can make testing harder.
- Using
AddSingletonfor services that hold state and should be created per request, causing unexpected shared data. - Forgetting to build the
ServiceProviderbefore requesting services. - Requesting services before registration is complete.
csharp
/* Wrong: Registering implementation only */ services.AddSingleton<ConsoleMessageService>(); /* Right: Register interface with implementation */ services.AddSingleton<IMessageService, ConsoleMessageService>();
Quick Reference
Here is a quick summary of service lifetimes:
| Method | Lifetime | When to Use |
|---|---|---|
| AddSingleton | One instance for app lifetime | Stateless or shared data services |
| AddScoped | One instance per client request | Services with request-specific data |
| AddTransient | New instance every time | Lightweight, stateless services |
Key Takeaways
Register services in C# using IServiceCollection methods like AddSingleton, AddScoped, or AddTransient.
Always register the service interface with its implementation for better flexibility and testing.
Choose the correct lifetime based on how long the service instance should live.
Build the ServiceProvider after registration before requesting services.
Avoid sharing state unintentionally by picking the right service lifetime.