.NET Core IoC RegisterAssemblyTypes Equivalant .NET Core IoC RegisterAssemblyTypes Equivalant asp.net asp.net

.NET Core IoC RegisterAssemblyTypes Equivalant


Yes you can do it with the built-in .NET Core IOC container, using Scrutor extension methods. It has got some nice assembly scanning functionalities.

Try this:

services.Scan(scan => scan                .FromAssemblies(typeof(yourassembly).GetTypeInfo().Assembly)                .AddClasses()                .AsImplementedInterfaces()                .WithScopedLifetime());

It applies on the built-in IOC container, though it's not itself a built-in package (Scrutor package on Nuget):


You can easily implement your own method to register all assembly types for a given assembly or set of assemblies... the code would be along the lines of:

foreach (var implementationType in assemblies.SelectMany(assembly => assembly.GetTypes()).Where(type => !type.GetTypeInfo().IsAbstract)){    foreach(var interfaceType in implementationType.GetInterfaces())    {        services.AddSingleton(interfaceType, implementationType);    }}

The code selects all non-abstract types from assemblies and retrieves all interfaces for each type creating a Singleton registration for each interface/implementation pair.


I prefer to register all instances of an explicit interface (i.e., ICommandHandler or similar), so I add extension methods along the lines of AddCommandHandlers shown below for the few types I want any instance of to be registered...

public static void AddCommandHandlers(this IServiceCollection services, params Assembly[] assemblies){    var serviceType = typeof(ICommandHandler);    foreach (var implementationType in assemblies.SelectMany(assembly => assembly.GetTypes()).Where(type => serviceType.IsAssignableFrom(type) && !type.GetTypeInfo().IsAbstract))    {        services.AddSingleton(serviceType, implementationType);    }}

Adding a call similar to services.AddCommandHandlers(DomainAssembly.Reference); in ConfigureServices...

I prefer this approach, because registering all interfaces for all types will add a lot of cruft registrations to your IoC container... typically not a huge deal, but cleaner in my opinion.


I can offer my assembly for type binding Nuget Q101.ServiceCollectionExtensionsmaybe it will be convenient to use.

            services.RegisterAssemblyTypes(typeof(ITypeInterface).Assembly)            .Where(t => t.Name.EndsWith("EndNameOfType")                     && t.GetInterfaces()                        .Any(ti => ti.Name == typeof(ITypeInterface).Name))            .AsScoped()    // Similarly Like As services.AddScoped(T1, T2)            .PropertiesAutowired() // Set properties values                            .Bind();        ...        services.RegisterAssemblyTypesByName(typeof(IStudentRepository).Assembly,            name => name.EndsWith("Repository")) // Condition for name of type            .AsScoped()    // Similarly Like As services.AddScoped(T1, T2)            // Set binding like as services.AddScoped<IRepository, Repository>();            .AsImplementedInterfaces()                             .Bind();

there is a small instruction manual on the github repositoryhttps://github.com/Axelweaver/q101-net-core22-service-collection-extensions