Autofac 6.0.0升级后注册自定义泛型抽象类报泛型类型定义错误
Autofac 6.0.0 泛型抽象类注册异常排查与解决方案
问题背景
- 当前使用 Autofac 6.0.0 版本,扫描注册程序集时触发运行异常,相同注册逻辑在 Autofac 3.5.0 版本下可正常运行
- 触发异常的自定义泛型抽象类定义如下:
public abstract class wndArbolVM<TGrupo, TNodoGrupo, THoja, TNodoHoja> : ViewModelBase where TGrupo : class, IIdEntero, IPadreId, new() where THoja : class,IIdEntero, IGrupoId, new() where TNodoGrupo : TreeItemHierarchicalVM, new() where TNodoHoja : TreeItemLeafVM, new() { }
报错信息
最常见的报错提示:
Type wndArbolVM`4[TGrupo,TNodoGrupo,THoja,TNodoHoja] is a generic type definition
完整异常堆栈跟踪:
en System.Dynamic.Utils.TypeUtils.ValidateType(Type type) en System.Linq.Expressions.Expression.New(ConstructorInfo constructor, IEnumerable`1 arguments) en Autofac.Core.Activators.Reflection.ConstructorBinder.GetConstructorInvoker(ConstructorInfo constructorInfo) en System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory) en Autofac.Core.Activators.Reflection.ConstructorBinder..ctor(ConstructorInfo constructorInfo) en Autofac.Core.Activators.Reflection.ReflectionActivator.ConfigurePipeline(IComponentRegistryServices componentRegistryServices, IResolvePipelineBuilder pipelineBuilder) en Autofac.Core.Registration.ComponentRegistration.BuildResolvePipeline(IComponentRegistryServices registryServices, IResolvePipelineBuilder pipelineBuilder) en Autofac.Core.Registration.ComponentRegistration.BuildResolvePipeline(IComponentRegistryServices registryServices) en Autofac.Core.Registration.ComponentRegistryBuilder.Build() en Autofac.ContainerBuilder.Build(ContainerBuildOptions options) en App.Application_Startup(Object sender, StartupEventArgs e) en en System.Windows.Application.OnStartup(StartupEventArgs e) en System.Windows.Application.<.ctor>b__1_0(Object unused) en System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) en System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
当前注册逻辑
var builder = new ContainerBuilder(); Assembly[] ensamblados = { Assembly.GetExecutingAssembly(), Assembly.GetAssembly(typeof(Configurator)), Assembly.GetAssembly(typeof(BaseAPI)), Assembly.GetCallingAssembly() }; builder.RegisterAssemblyTypes(ensamblados).Where(p => p.BaseType == typeof(BaseAPI)).SingleInstance(); builder.RegisterByAttributes(ensamblados); builder.RegisterType<wndPrincipal>().SingleInstance(); builder.RegisterType<Sesion>().SingleInstance(); builder.RegisterInstance(LogManager.GetCurrentClassLogger()).As<ILogger>(); builder.RegisterInstance(new Entorno(Entorno.GESTION)).As<Entorno>(); builder.RegisterInstance(this).As<App>(); Container = builder.Build();
异常根因
Autofac 6.0 对注册流程的类型校验做了严格化调整:
- 从堆栈定位,异常发生在
ContainerBuilder.Build阶段的反射激活器初始化环节。当前注册链路中,自定义的RegisterByAttributes扩展在程序集扫描时,误把wndArbolVM<,,,>这个开放泛型定义当成具体可实例化类型完成了注册。 - 开放泛型定义(未绑定具体泛型参数的泛型类型)本身无法直接调用构造函数实例化,Autofac 6.0 在通过
Expression.New绑定构造函数时,会强制校验传入类型必须是可实例化的封闭类型,因此直接抛出类型校验异常。 - Autofac 3.5.0 版本没有这类强制校验逻辑,会静默跳过不符合要求的注册项,因此相同逻辑在旧版本下运行不会触发报错。
注:该类本身是抽象类,即便绑定了具体泛型参数,也无法被直接实例化,正常场景下不需要单独注册这个开放泛型定义。
落地解决方案
- 优先定位错误注册来源
临时注释掉RegisterByAttributes(ensamblados)这行注册代码,重新执行Build。如果异常消失,即可确认问题来自该自定义扩展的扫描逻辑,只需要在扩展的类型过滤规则中,追加排除开放泛型定义的条件即可,示例修改:// 在RegisterByAttributes的扫描逻辑中增加过滤条件 builder.RegisterByAttributes(ensamblados) .Where(t => !t.IsGenericTypeDefinition); - 若确实需要保留该泛型类的依赖服务,使用Autofac 开放泛型的正确注册方式
不能直接将开放泛型定义作为普通类型注册,需要调用RegisterGeneric方法完成泛型服务配置,且必须存在对应的非抽象派生类(绑定具体泛型参数)才能被正常解析:builder.RegisterGeneric(typeof(wndArbolVM<,,,>)) .AsSelf() .InstancePerDependency(); - 常规扫描逻辑补全
给所有自定义程序集扫描逻辑统一追加排除规则,跳过以下类型:- 开放泛型定义(
t.IsGenericTypeDefinition == true) - 抽象类本身(
t.IsAbstract == true)
避免后续其他泛型/抽象类被误注册触发同类问题。
- 开放泛型定义(
内容的提问来源于stack exchange,提问作者t00n
相关产品推荐
相关产品推荐

