Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用ASP.NET 5中的默认DI容器(类似于Autofac)一次性注册所有服务_Asp.net_Dependency Injection_Asp.net Core_Repository Pattern_Asp.net Core Mvc - Fatal编程技术网

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

使用ASP.NET 5中的默认DI容器(类似于Autofac)一次性注册所有服务,asp.net,dependency-injection,asp.net-core,repository-pattern,asp.net-core-mvc,Asp.net,Dependency Injection,Asp.net Core,Repository Pattern,Asp.net Core Mvc,在ASP.NET5中,已经有一个DI作为默认值提供,看起来很有趣。我一直在使用Autofac和MVC5,它可以选择一次注册所有程序集。下面是一个示例代码,用于注册Autofac中以“Service”结尾的所有类 // Autofac Configuration for API var builder = new ContainerBuilder(); builder.RegisterModule(new ServiceModule()); ... ... builder.RegisterA

在ASP.NET5中,已经有一个DI作为默认值提供,看起来很有趣。我一直在使用Autofac和MVC5,它可以选择一次注册所有程序集。下面是一个示例代码,用于注册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.NET5的默认DI容器中是否有类似的东西,您可以在其中一次注册所有服务

我的问题是,在asp.NET5的默认DI容器中是否有类似的东西,您可以在其中一次注册所有服务

不是OOTB,而是Kristian Hellang创建了一个名为的很酷的包,该包为内置容器添加了程序集扫描功能。你可以在电视上找到它

services.Scan(扫描=>Scan
.FromAssemblyOf()
.AddClasses(classes=>classes.AssignableTo())
.AsImplementedInterfaces()
.WithTransientLifetime()
.AddClasses(classes=>classes.AssignableTo())
.As()
.使用ScopedLifeTime());

您仍然可以将
Autofac
与Asp.net(5/Core)或MVC6一起使用。您需要更改
ConfigureServices
方法的签名,以返回
IServiceProvider
并将参数设置为
IServiceCollection

例如

公共IServiceProvider配置服务(IServiceCollection服务)
{
//我们在这里而不是在ConfigureServices中添加MVC。
services.AddMvc();
//创建Autofac容器生成器。
var builder=new ContainerBuilder();
//添加任何Autofac模块或注册。
RegisterModule(新的AutofacModule());
//填充服务。
建造商。填充(服务);
//构建容器。
var container=builder.Build();
//解析并返回服务提供商。
返回container.Resolve();
}

请参阅有关将Autofac与Asp.net(5/Core)集成的更多详细信息。

您可能会惊讶地发现,您所需的大部分内容都可以在几行代码中完成,而无需依赖第三方软件包!。。一旦其他人弄明白了这几行应该是什么,scrutor会与.NETCore2.0一起工作吗?我不能用nuget安装它。@mac10688是的,应该安装。它的目标是netstandard1.0和1.6,这两个版本都应该在.NETCore2.0上运行。你看到的问题是什么?你能在GitHub上提交一个问题吗?如果我参加聚会有点晚了,很抱歉,但我猜没有NuGet软件包,没有其他方法可以做到这一点?我很好奇这是否已经更新了
services.Scan(scan => scan
    .FromAssemblyOf<ITransientService>()
        .AddClasses(classes => classes.AssignableTo<ITransientService>())
            .AsImplementedInterfaces()
            .WithTransientLifetime()
        .AddClasses(classes => classes.AssignableTo<IScopedService>())
            .As<IScopedService>()
            .WithScopedLifetime());
 public IServiceProvider ConfigureServices(IServiceCollection services)
 {
     // We add MVC here instead of in ConfigureServices.
     services.AddMvc();

     // Create the Autofac container builder.
     var builder = new ContainerBuilder();

     // Add any Autofac modules or registrations.
     builder.RegisterModule(new AutofacModule());

     // Populate the services.
     builder.Populate(services);

     // Build the container.
     var container = builder.Build();

     // Resolve and return the service provider.
     return container.Resolve<IServiceProvider>();
 }