Search code examples
asp.net-web-api.net-6.0inversion-of-control

.NET 6 IoC register services from another assembly


I'd like to register some services from another assembly with built-in IoC of .NET 6. Is it possible as Autofac does?

I found others nuget extensions, still not sure what it is the best practice

Thanks


Solution

  • Scrutor is the way. Just tried this example

    var assembly = Assembly.LoadFile(@"path\stuff.dll");
    
    builder.Services.Scan(scan => scan.FromAssemblies(assembly)
                .AddClasses(classes => classes.AssignableTo<IMyInterface>())
                .AsImplementedInterfaces()
                .WithScopedLifetime());
    

    It works, easier. Fantastic!