使用类似于Autofac的ASP.NET 5中的默认DI容器一次性注册所有服务 [英] Register all the services at once using Default DI Container from ASP.NET 5 similar to Autofac
问题描述
使用ASP.NET 5,默认情况下已经存在一个DI,它看起来很有趣.我一直在将Autofac与MVC 5配合使用,该选项具有一次注册所有程序集的选项.这是一个示例代码,在Autofac中注册了所有以"Service"结尾的类.
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();
我的问题是,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 ?
推荐答案
我的问题是,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 ?
不是OOTB,而是Kristian Hellang创建了一个很酷的程序包,名为 Scrutor ,该程序包将程序集扫描功能添加到了内置容器.您可以在 NuGet.org 上找到它.
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());
这篇关于使用类似于Autofac的ASP.NET 5中的默认DI容器一次性注册所有服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!