【问题标题】:Autofac with nested open generics具有嵌套开放泛型的 Autofac
【发布时间】:2016-04-16 14:24:14
【问题描述】:

我是 Autofac 的新手,在解析嵌套的开放式通用服务类型时遇到了一些麻烦。

我希望我的ContactService 实现两个接口。两个接口都使用相同的泛型类型参数,但第二个是嵌套的:

class ContactService<TParent>
    : IContactService<TParent>,
      IFeatureProvider<ContactFeature<TParent>>
    where TParent : class, IDomainModel
{ }

如果我这样做:

builder
    .RegisterGeneric(typeof(ContactService<>))
    .As(typeof(IContactService<>))
    .As(typeof(IFeatureProvider<>);

然后尝试解析服务IFeatureProvider&lt;ContactFeature&lt;Household&gt;&gt;(例如)导致Autofac尝试实例化

ContactService<ContactFeature<Household>>

而不是

ContactService<Household>

我明白为什么上述方法不起作用。所以我尝试了这个:

builder
    .RegisterGeneric(typeof(ContactService<>))
    .As(typeof(IContactService<>))
    .As(typeof(IFeatureProvider<>)
        .MakeGenericType(typeof(ContactFeature<>)));

但是,我仍然收到类似的错误:

TypeLoadException: GenericArguments[0], 'ContactFeature`1[Household]', 
  on 'Contact`1[TParent]' violates the constraint of type parameter 'TParent'.

System.RuntimeTypeHandle.Instantiate(RuntimeTypeHandle handle, IntPtr* pInst, 
  Int32 numGenericArgs, ObjectHandleOnStack type)

ArgumentException: GenericArguments[0], 'ContactFeature`1[Household]', 
  on 'IContactService`1[TParent]' violates the constraint of type 'TParent'.

System.RuntimeType.ValidateGenericArguments(MemberInfo definition, 
  RuntimeType[] genericArguments, Exception e)

看起来 Autofac 试图将 ContactFeature&lt;Household&gt; 传递为 TParent 而不仅仅是 Household

抱歉,解释太长了。我希望有人能帮我解决这个问题!

谢谢。

【问题讨论】:

    标签: c# .net generics autofac


    【解决方案1】:

    看来您所要做的就是将您打开的通用注册拆分为 2 个单独的注册,如下所示:

    var builder = new ContainerBuilder();
    builder.RegisterGeneric(typeof(ContactService<>))
        .As(typeof(IContactService<>));
    
    builder.RegisterGeneric(typeof(ContactService<>))
        .As(typeof(IFeatureProvider<>));
    
    var container = builder.Build();
    
    var service = container.Resolve(typeof(IFeatureProvider<ContactFeature<Household>>));
    var service2 = container.Resolve(typeof(IContactService<Household>));
    
    Console.WriteLine(service.GetType());
    Console.WriteLine(service2.GetType());
    
    Console.ReadKey();
    

    两个解析都会给你ContactService&lt;Household&gt; 的实例。但老实说,我不知道为什么它单独工作,当你把它放在一起注册时,它不起作用。

    我使用了以下类和接口来测试解决方案:

    class ContactService<TParent> : IContactService<TParent>, IFeatureProvider<ContactFeature<TParent>>
        where TParent : class, IDomainModel
    {
    }
    
    interface IContactService<T> where T : class, IDomainModel
    {
    }
    
    interface IFeatureProvider<TProvider>
    {
    }
    
    interface IDomainModel
    {
    }
    
    class Household : IDomainModel
    {
    }
    
    class ContactFeature<TDomainModel> where TDomainModel: class, IDomainModel
    {
    }
    

    【讨论】:

    • 非常感谢...没想到这么简单!你认为这可能是 Autofac 的泛型类型检查中的一个错误吗?
    猜你喜欢
    • 1970-01-01
    • 2010-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-22
    • 1970-01-01
    相关资源
    最近更新 更多