Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.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
C# Autofac覆盖泛型类型的上次注册_C#_Generics_Dependency Injection_Autofac - Fatal编程技术网

C# Autofac覆盖泛型类型的上次注册

C# Autofac覆盖泛型类型的上次注册,c#,generics,dependency-injection,autofac,C#,Generics,Dependency Injection,Autofac,我试图用Autofac注册泛型类型,但最后一个值最终覆盖了前一个值。公共项目具有实体服务接口 public interface IEntityService<TEntity> where TEntity : class { TEntity GetByID(object id); } EFModule类 public class EFModule : Module { protected override void Load(ContainerBuilder buil

我试图用Autofac注册泛型类型,但最后一个值最终覆盖了前一个值。公共项目具有实体服务接口

public interface IEntityService<TEntity> where TEntity : class
{
    TEntity GetByID(object id);
}
EFModule类

public class EFModule : Module
{
    protected override void Load(ContainerBuilder builder)
    {
      builder.RegisterGeneric(typeof(Service.EntityService<>))
      .As(typeof(Common.IEntityService<>))
      .Named(Common.ConfigType.ProjectType.Main,typeof(Common.IEntityService<>))
      .InstancePerDependency();

      builder.RegisterGeneric(typeof(LogProject.Service.EntityService<>))
      .As(typeof(Common.IEntityService<>))
      .Named(Common.ConfigType.ProjectType.Log, typeof(Common.IEntityService<>))
      .InstancePerDependency();
   }
}
公共类EFModule:Module
{
受保护的覆盖无效负载(ContainerBuilder builder)
{
builder.RegisterGeneric(typeof(Service.EntityService))
.As(类型(通用.智能服务))
.Named(Common.ConfigType.ProjectType.Main,typeof(Common.IEntityService))
.InstancePerDependence();
builder.RegisterGeneric(typeof(LogProject.Service.EntityService))
.As(类型(通用.智能服务))
.Named(Common.ConfigType.ProjectType.Log,typeof(Common.IEntityService))
.InstancePerDependence();
}
}
所发生的事情是,无论最后的配置是什么,所有这些都可以正常工作。喂,我把

builder.RegisterGeneric(typeof(LogProject.Service.EntityService<>))
builder.RegisterGeneric(typeof(LogProject.Service.EntityService))

在web.config中,项目类型为
LogProject
。然后它工作正常,但是如果我将
MainProject
放在配置文件中。反之亦然,如果
web.config
中的最后一行(
Main
Log
)与
module
中的最后一行不相同,则覆盖将抛出异常。基本上,泛型不是基于but进行解析,而是被最后一个值覆盖。

您正在注册
typeof(Common.IEntityService)
的两个实例。最后注册的实例获胜是正常和预期的行为

Per:

如果多个组件公开同一服务,Autofac将使用最后注册的组件作为该服务的默认提供程序

如果您希望Autofac能够区分这两者,您有两个选项:

创建单独的抽象
公共接口ILogEntityService:IEntityService其中TEntity:class
{
}
将上述界面用于日志项目的构造函数参数,并按如下方式进行注册:

builder.RegisterGeneric(typeof(Service.EntityService<>))
  .As(typeof(Common.IEntityService<>))
  .Named(Common.ConfigType.ProjectType.Main,typeof(Common.IEntityService<>))
  .InstancePerDependency();

builder.RegisterGeneric(typeof(LogProject.Service.EntityService<>))
  .As(typeof(ILogEntityService<>))
  .Named(Common.ConfigType.ProjectType.Log, typeof(Common.IEntityService<>))
  .InstancePerDependency();
builder.RegisterGeneric(typeof(Service.EntityService))
.As(类型(通用.智能服务))
.Named(Common.ConfigType.ProjectType.Main,typeof(Common.IEntityService))
.InstancePerDependence();
builder.RegisterGeneric(typeof(LogProject.Service.EntityService))
.As(类型(ILogEntityService))
.Named(Common.ConfigType.ProjectType.Log,typeof(Common.IEntityService))
.InstancePerDependence();
使用混凝土类型/组件
builder.RegisterGeneric(typeof(Service.EntityService))
.AsSelf()
.Named(Common.ConfigType.ProjectType.Main,typeof(Common.IEntityService))
.InstancePerDependence();
builder.RegisterGeneric(typeof(LogProject.Service.EntityService))
.AsSelf()
.Named(Common.ConfigType.ProjectType.Log,typeof(Common.IEntityService))
.InstancePerDependence();
然后在服务构造函数中使用具体类型

public SomeService(ConcreteEntityService<SomeClass> foo)
publicsomeservice(具体实体服务foo)
当然,就能够交换/模拟实现而言,使用第一个选项更好

builder.RegisterGeneric(typeof(LogProject.Service.EntityService<>))
public interface ILogEntityService<TEntity> : IEntityService<TEntity> where TEntity : class
{
}
builder.RegisterGeneric(typeof(Service.EntityService<>))
  .As(typeof(Common.IEntityService<>))
  .Named(Common.ConfigType.ProjectType.Main,typeof(Common.IEntityService<>))
  .InstancePerDependency();

builder.RegisterGeneric(typeof(LogProject.Service.EntityService<>))
  .As(typeof(ILogEntityService<>))
  .Named(Common.ConfigType.ProjectType.Log, typeof(Common.IEntityService<>))
  .InstancePerDependency();
builder.RegisterGeneric(typeof(Service.EntityService<>))
  .AsSelf()
  .Named(Common.ConfigType.ProjectType.Main,typeof(Common.IEntityService<>))
  .InstancePerDependency();

builder.RegisterGeneric(typeof(LogProject.Service.EntityService<>))
  .AsSelf()
  .Named(Common.ConfigType.ProjectType.Log, typeof(Common.IEntityService<>))
  .InstancePerDependency();
public SomeService(ConcreteEntityService<SomeClass> foo)