C# Autofac覆盖泛型类型的上次注册
我试图用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
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)