.NET Core IOC RegisterAssemblyTypes Equivalant

I use the standard built-in .NET IOC core. But I need functionality like AutoFac. I need / need to limit the number of third-party dependencies in this project. So I was hoping I could do something similar to this AutoFac method in the standard .NET Core IOC.

builder.RegisterAssemblyTypes(assemblyhere).AsImplementedInterfaces(); 

Is it possible?

+8
asp.net-core autofac asp.net-core-mvc
source share
3 answers

Yes, you can do this with the built-in IQ.NET Core container using Scrutor extension methods. It has some assembly scan features.

Try the following:

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

It applies to the IOC inline container, although it is not an inline package itself ( the Scrutor package on Nuget ):

+3
source share

Perhaps you can implement your own extension method to do something similar, but you can also just use Autofac if it has the function you want. Do you have any reason? To do this, simply configure it and return it with ConfigureServices , as shown here .

 public IServiceProvider ConfigureServices(IServiceCollection services) { services.AddMvc(); // Add other framework services // Add Autofac var containerBuilder = new ContainerBuilder(); containerBuilder.RegisterModule<DefaultModule>(); containerBuilder.Populate(services); var container = containerBuilder.Build(); return new AutofacServiceProvider(container); } 
0
source share

You can easily implement your own method of registering all types of assemblies for a given assembly or set of assemblies ... the code will correspond to the lines:

 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 extracts all the interfaces for each type, creating a Singleton registration for each interface / implementation pair.


I prefer to register all instances of the explicit interface (i.e., ICommandHandler or similar), so I add extension methods along the AddCommandHandlers lines shown below for several types that I want any instance 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 like services.AddCommandHandlers(DomainAssembly.Reference); in ConfigureServices ...

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

0
source share

All Articles