Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.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 core 如何在Scrutor Simular StructureMap中注册程序集上的所有接口_Asp.net Core_Dependency Injection_.net Core_Inversion Of Control_Scrutor - Fatal编程技术网

Asp.net core 如何在Scrutor Simular StructureMap中注册程序集上的所有接口

Asp.net core 如何在Scrutor Simular StructureMap中注册程序集上的所有接口,asp.net-core,dependency-injection,.net-core,inversion-of-control,scrutor,Asp.net Core,Dependency Injection,.net Core,Inversion Of Control,Scrutor,如何使用scan扩展注册程序集中的所有接口,而无需在ASP.NET Core 2中分开写入所有接口 在StructureMap中: Scan(=> { //声明要扫描的程序集 _.组装(“结构图测试”); }); 在Scrutor中: collection.Scan(扫描=>Scan //我们从ITransientService程序集中的所有类型开始 .FromAssemblyOf() //AddClasses从本文档中的所有公共、非抽象类型开始 //这些类型然后由传递给 //在这种情况下,我

如何使用
scan
扩展注册程序集中的所有接口,而无需在ASP.NET Core 2中分开写入所有接口

在StructureMap中:

Scan(=>
{
//声明要扫描的程序集
_.组装(“结构图测试”);
});
在Scrutor中:

collection.Scan(扫描=>Scan
//我们从ITransientService程序集中的所有类型开始
.FromAssemblyOf()
//AddClasses从本文档中的所有公共、非抽象类型开始
//这些类型然后由传递给
//在这种情况下,我们只过滤掉可赋值的类
//到ITransientService。
.AddClasses(classes=>classes.AssignableTo())
//然后,我们指定要注册这些类的类型。
//在本例中,我们希望将这些类型注册为所有实现的类型
//接口。所以,如果一个类型实现了3个接口:a、B、C,我们就结束了
//总共有三个独立的注册。
.AsImplementedInterfaces()
//最后,我们指定这些注册的生存期。
.WithTransientLifetime()
//在这里,我们从程序集中的一整套新类开始
//这一次,只过滤出可分配给
//IScopedService。
.AddClasses(classes=>classes.AssignableTo())
//现在,我们只想将这些类型注册为单个接口,
//IScopedService。
.As()
//同样,只需指定生存期。
.使用ScopedLifeTime());

这将注册实现某些接口的所有类,默认情况下,StructureMap会这样做:

services.Scan(scan => scan
    .FromAssemblyOf<IService>()
    .AddClasses()
    .AsImplementedInterfaces()
    .WithTransientLifetime());
services.Scan(扫描=>Scan
.FromAssemblyOf()
.AddClasses()
.AsImplementedInterfaces()
.WithTransientLifetime());

对于您可以定义的所有类型的使用寿命时间

services.Scan(scan => scan
      .FromAssemblyOf<IApplicationService>()

      .AddClasses(classes => classes.AssignableTo<IScopedDependency>())
      .AsMatchingInterface()
      .WithScopedLifetime()

      .AddClasses(classes => classes.AssignableTo<ISingletonDependency>())
      .AsMatchingInterface()
      .WithSingletonLifetime()

      .AddClasses(classes => classes.AssignableTo<ITransientDependency>())
      .AsMatchingInterface()
      .WithTransientLifetime()
);
对于接口中的示例

public interface IUserService : IScopedDependency { }
public class UserService: IUserService { }
public interface IUserService : IScopedDependency { }
public class UserService: IUserService { }