EasyServiceRegister 3.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package EasyServiceRegister --version 3.0.0
                    
NuGet\Install-Package EasyServiceRegister -Version 3.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="EasyServiceRegister" Version="3.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="EasyServiceRegister" Version="3.0.0" />
                    
Directory.Packages.props
<PackageReference Include="EasyServiceRegister" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add EasyServiceRegister --version 3.0.0
                    
#r "nuget: EasyServiceRegister, 3.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=EasyServiceRegister&version=3.0.0
                    
Install as a Cake Addin
#tool nuget:?package=EasyServiceRegister&version=3.0.0
                    
Install as a Cake Tool

EasyServiceRegister - A easy service registration library

EasyServiceRegister is a library that simplifies the process of registering services with the .NET Core Dependency Injection framework. With version 3.0.0, we have introduced optimizations and powerful new features.

More Details below!!

How to use it:

  1. Install the EasyServiceRegister package in your project:
  dotnet add package EasyServiceRegister
  1. Add the appropriate class attribute to your service class to indicate how it should be registered with DI:
RegisterAsSingleton  --> It will register your service as Singleton. 
RegisterAsSingletonKeyed --> It will register your service as Singleton with a key. 
RegisterAsScoped    --> It will register your service as Scoped. 
RegisterAsScopedKeyed --> It will register your service as Scoped with a key. 
RegisterAsTransient --> It will register your service as Transient. 
RegisterAsTransientKeyed --> It will register your service as Transient with a key.

Note: You can use an optional parameter (useTryAddSingleton,useTryAddScopped or useTryAddTranscient) with the attribute to indicate whether to use TryAdd or Add when registering the service with DI.

  1. Register your services by calling the AddServices extension method on the IServiceCollection instance in your Startup.cs or Program.cs file:
services.AddServices(params Type[] handlerAssemblyMarkerTypes);

//Example: services.AddServices(typeof(AssemblyContainingService), typeof(AnotherAssemblyContainingService));

This method takes an array of marker types that indicate which assemblies to scan for service implementations.

In case your service class implements an interface to abstract itself, the interface must be the last interface implemented.

Examples:

Here is an example of a service implementation that registers itself as a scoped service:

[RegisterAsScoped] 
public class ProductCommandServices : IProductCommandServices 
{ 
	public Task<Product> CreateProduct(Product product) 
	{ 
		// Implementation for creating a product 
	} 
}

And here is an example of a service that registers itself as a singleton service using 'TryAddSingleton'

[RegisterAsSingleton(useTryAddSingleton: true)] 
public class GetCurrentUser 
{ 
	public Task<User> GetCurrentUser() 
	{ 
		// Implementation for getting the current user 
	} 
}

Keyed Services (Available in .NET 8+)

Register and resolve services with specific keys:

[RegisterAsSingletonKeyed("primary")] 
public class PrimaryEmailService : IEmailService 
{ 
	// Implementation 
}

[RegisterAsSingletonKeyed("secondary")] 
public class SecondaryEmailService : IEmailService 
{ 
	// Implementation 
}

// Consume keyed services 
public class EmailManager 
{ 
	private readonly IEmailService _primaryService; 
	private readonly IEmailService _secondaryService;

	public EmailManager([FromKeyedServices("primary")] IEmailService primaryService, 
	                    [FromKeyedServices("secondary")] IEmailService secondaryService)
	{
	    _primaryService = primaryService;
	    _secondaryService = secondaryService;
	}
}

Decorator Pattern Support

Easily implement the decorator pattern for your services:

public interface INotificationService 
{ 
  void SendNotification(string message); 
}

[RegisterAsScoped] 
[DecorateWith(typeof(LoggingNotificationDecorator), order: 0)]
public class EmailNotificationService : INotificationService 
{ 
  public void SendNotification(string message) 
  { 
    // Send email notification 
  } 
}

public class LoggingNotificationDecorator : INotificationService 
{ 
  private readonly INotificationService _inner; 
  
  private readonly ILogger _logger;
  
  public LoggingNotificationDecorator(INotificationService inner, ILogger logger)
  {
      _inner = inner;
      _logger = logger;
  }

  public void SendNotification(string message)
  {
      _logger.Log($"Sending notification: {message}");
      _inner.SendNotification(message);
  }
}

Registration Diagnostics

Get insights into your service registrations:

var diagnostics = services.GetRegistrationDiagnostics();

foreach (var info in diagnostics) 
{ 
  Console.WriteLine($"Service: {info.ServiceType}, Implementation: {info.ImplementationType}, Lifetime: {info.Lifetime}"); 
}

Anti-Pattern Validation

EasyServiceRegister helps you avoid common DI anti-patterns by validating your registrations:

var validationIssues = builder.Services.ValidateServices();

foreach (var issue in validationIssues)
{
    Console.WriteLine(issue);
}

With EasyServiceRegister, you can simplify your code by eliminating the need for huge extension methods and a cluttered Startup class.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.0.1 0 7/4/2025
3.0.0 0 7/4/2025
2.2.1 191 6/1/2025
2.2.0 138 6/1/2025
2.1.0 342 5/1/2025
2.0.9 7,269 12/6/2023
2.0.8 170 12/5/2023
2.0.7 129 12/5/2023
2.0.6 803 4/29/2023
2.0.5 2,676 11/26/2022
2.0.4 353 11/23/2022
2.0.3 382 11/20/2022
2.0.2 357 11/19/2022
2.0.1 337 11/19/2022
2.0.0 350 11/19/2022
1.0.2 2,959 5/8/2022
1.0.1 478 5/8/2022
1.0.0 622 1/19/2022
0.0.9 440 1/20/2022
0.0.8 468 1/20/2022