如何使用.net核心默认服务容器注册特定接口的所有类

Ani*_*aha 6 c# dependency-injection asp.net-core-mvc asp.net-core

在统一中,我可以使用这种方式从程序集中注册所有类型的接口.

    public static void RegisterTypes(IUnityContainer container)
    {

        container.RegisterTypes(
            AllClasses.FromLoadedAssemblies().
                Where(
                    type =>
                        typeof (IRunAtInit).IsAssignableFrom(type),
            WithMappings.FromAllInterfaces,
            WithName.TypeName);

              }
Run Code Online (Sandbox Code Playgroud)

是否有可能使用自己的默认服务容器以这种方式实现.net核心?

gpa*_*oli 5

尝试使用Scrutor扩展:https : //github.com/khellang/Scrutor

services.Scan(scan => scan
    .FromAssemblyOf<IRunAtInit>()
    .AddClasses(classes => classes.AssignableTo<IRunAtInit>())
    .AsImplementedInterfaces()
    .WithTransientLifetime());
Run Code Online (Sandbox Code Playgroud)