Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Register all the services at once using Default DI Container from ASP.NET 5 similar to Autofac

With ASP.NET 5, there's already a DI shipped as default and it looks interesting. I have been using Autofac with MVC 5 which has the option to register all the assembly at once. Here is a sample code that register all the classes that ends with "Service" in Autofac.

// Autofac Configuration for API
var builder = new ContainerBuilder();

builder.RegisterModule(new ServiceModule());

...
...

builder.RegisterAssemblyTypes(Assembly.Load("IMS.Service"))
                      .Where(t => t.Name.EndsWith("Service"))
                      .AsImplementedInterfaces()
                      .InstancePerLifetimeScope();

My question is, is there any thing similar to this in Default DI container from asp.net 5 where you can register all the services at once ?

like image 491
prashantchalise Avatar asked Feb 18 '16 18:02

prashantchalise


People also ask

What is Autofac in ASP.NET Core?

Autofac is a . Net-based IoC container. When classes interact with one another, it manages the dependencies between them that allow applications to remain flexible as they grow in size and complexity. Autofac is the most widely used DI/IoC container for ASP.NET, and it is fully compatible with.NET Core as well.

Which method is used to register dependent types services with LOC container?

Using method "GetService" of the "HttpContext. RequestServices" property, we can get dependent services configured with the Service container. This is also known as property injection.


2 Answers

It might surprise you that most of what you need can be done in few lines without reliance on third-party packages!.. Once someone else has figured out what those few lines should be 😀

For example, here is a handful of concise DI extensions from Autofac and NetCore.AutoRegisterDi (all methods are annotated with source links). It lets you write registration logic like (example):

var types = typeof(BaseService<>).Assembly
                        .GetTypes()
                        .Where(t =>
                                !t.IsAbstract &&
                                t.IsClosedTypeOf(typeof(BaseService<>)))
                        ).ToList();
services.RegisterAsImplementedInterfaces(types, ServiceLifetime.Scoped);

This blog post, .NET Core project without Autofac, has more examples and covers limitations if you want take one level further.

like image 88
Alex Klaus Avatar answered Oct 01 '22 07:10

Alex Klaus


My question is, is there any thing similar to this in Default DI container from asp.net 5 where you can register all the services at once ?

Not OOTB, but Kristian Hellang created a cool package named Scrutor that adds assembly scanning capabilities to the built-in container. You can find it on NuGet.org.

services.Scan(scan => scan
    .FromAssemblyOf<ITransientService>()
        .AddClasses(classes => classes.AssignableTo<ITransientService>())
            .AsImplementedInterfaces()
            .WithTransientLifetime()
        .AddClasses(classes => classes.AssignableTo<IScopedService>())
            .As<IScopedService>()
            .WithScopedLifetime());
like image 24
Kévin Chalet Avatar answered Oct 01 '22 07:10

Kévin Chalet