TanvirArjel.Extensions.Microsoft.DependencyInjection

This will enable you to register all your services into .NET Dependency Injection container (IServiceCollection) at once without exposing the service implementation. For configuration details please visit the GitHub repository.


Keywords
.NET-5.0, .NET-6.0, .NET-7.0, .NET-Core, ASP.NET-Core, DependencyInjection, Dynamic-Service-Registration
License
MIT
Install
Install-Package TanvirArjel.Extensions.Microsoft.DependencyInjection -Version 2.2.0

Documentation

👑 .NET/.NET Core Dynamic Service Registration

This is a NET 5.0 and .NET Core dynamic service registration library which enables you to register all your services into .NET 5.0 and .NET Core Dependency Injection container at once without exposing the service implementation.

Give a star

If you find this library useful to you, please don't forget to encouraging me to do such more stuffs by giving a star () to this repository. Thank you.

✈️ How do I get started?

First install the lastest version of TanvirArjel.Extensions.Microsoft.DependencyInjection nuget package into your project as follows:

Install-Package TanvirArjel.Extensions.Microsoft.DependencyInjection

Now in your ConfigureServices method of the Startup class:

public static void ConfigureServices(IServiceCollection services)
{
    services.AddServicesOfAllTypes();
}

Moreover, if you want only specific assemblies to be scanned during type scanning:

public static void ConfigureServices(IServiceCollection services)
{
    // Assemblies start with "TanvirArjel.Web", "TanvirArjel.Application" will only be scanned.
    string[] assembliesToBeScanned = new string[] { "TanvirArjel.Web", "TanvirArjel.Application" };
    services.AddServicesOfAllTypes(assembliesToBeScanned);
}

For Blazor WebAssembly App:

builder.Services.AddServicesOfAllTypes(Assembly.GetExecutingAssembly());

🛠️ Usage:

Now mark your services with any of the ScopedServiceAttribute, TransientServiceAttribute and SingletonServiceAttribute attributes as follows:

// Mark with ScopedServiceAttribute if you want to register `IEmployeeService` as scoped service.
[ScopedService]
public interface IEmployeeService
{
    Task CreateEmployeeAsync(Employee employee);
}

internal class EmployeeService : IEmployeeService 
{
    public async Task CreateEmployeeAsync(Employee employee)
    {
       // Implementation here
    };
}

For hosted service:

[HostedService]
public class TestBackgroundService : BackgroundService
{
    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        Console.WriteLine("Hosted service called.");
        await Task.CompletedTask.ConfigureAwait(false);
    }
}

🐞 Bug Report

Dont forget to submit an issue if you face. we will try to resolve as soon as possible.