Webapi依赖注入

时间:2017-08-23 10:07:56

标签: asp.net-web-api unity-container

HeJ小鼠,

我正在开发asp.net Webapi。当我尝试在我的存储库构造函数中注入值时,它会抛出异常。 解决依赖关系失败,

  

type =" Controllers.XXXXX",name ="(none)"

。在解决时发生异常:例外是:

  

InvalidOperationException - 当前类型IRepository是一个接口,无法构造。你错过了类型映射吗? -----------------------------------------------

我正在做那样的注册

string modelMapperName = Guid.NewGuid().ToString();
        container.RegisterType<IBulkCopy, BulkCopy>("bulkCopy", new InjectionConstructor("dbo", "test"));

        container.RegisterType<IMapConfigurator, ProductMap>(modelMapperName, new ContainerControlledLifetimeManager());

        container.RegisterType<IModelMapper, ModelAutoMapper>(modelMapperName,
            new ContainerControlledLifetimeManager(), new InjectionConstructor(new ResolvedParameter<IMapConfigurator>(modelMapperName)));

        container.RegisterType<IProductRepository, EProductRepository>("productRepository", new PerResolveLifetimeManager(),
             new InjectionConstructor(
                new ResolvedParameter<IRepositoryFactory>(DatabaseConnection.XXXX)
                , new ResolvedParameter<IModelMapper>(modelMapperName),
                new ResolvedParameter<IBulkCopy>("bulkCopy")));

当我从IProductRepository映射中删除ResolvedParamater时,它可以正常工作。任何人都可以帮助我找出这个问题。

提前致谢

0 个答案:

没有答案