加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

使用ASP.NET 5中的默认DI容器一次注册所有服务,类似于Autofac

发布时间:2020-12-16 03:43:15 所属栏目:asp.Net 来源:网络整理
导读:在ASP.NET 5中,默认情况下已经发布了一个DI,它看起来很有趣.我一直在使用Autofac和MVC 5,它可以选择立即注册所有程序集.下面是一个示例代码,用于注册Autofac中以“Service”结尾的所有类. // Autofac Configuration for APIvar builder = new ContainerBuild
在ASP.NET 5中,默认情况下已经发布了一个DI,它看起来很有趣.我一直在使用Autofac和MVC 5,它可以选择立即注册所有程序集.下面是一个示例代码,用于注册Autofac中以“Service”结尾的所有类.

// 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();

我的问题是,在asp.net 5的Default DI容器中是否有类似的东西,您可以立即注册所有服务?

解决方法

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 ?

不是OTB,而是Kristian Hellang创建了一个名为Scrutor的酷包,它为内置容器增加了组装扫描功能.你可以在NuGet.org找到它.

services.Scan(scan => scan
    .FromAssemblyOf<ITransientService>()
        .AddClasses(classes => classes.AssignableTo<ITransientService>())
            .AsImplementedInterfaces()
            .WithTransientLifetime()
        .AddClasses(classes => classes.AssignableTo<IScopedService>())
            .As<IScopedService>()
            .WithScopedLifetime());

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读