团结集装箱试图解决非注册的类型,抛出错误 [英] Unity Container trying to resolve non registered type, throwing error

查看:140
本文介绍了团结集装箱试图解决非注册的类型,抛出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我继承现有的ASP.Net MVC项目,利用一个统一的DI容器。它的所有注册都在web.config中定义的。

I've inherited an existing ASP.Net MVC project that makes use of a Unity DI container. All of its registrations are defined in web.config.

当一个新的服务/类被引入到项目中,我创建它与接口一起,然后更新配置文件办理登记。例如:

When a new service / class is introduced into the project, I create it along with an Interface, and then update the config file to handle the registration. For example :

<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
    <assembly name="UnityDi.Contracts" />
    <assembly name="UnityDi.Domain" />
    <assembly name="UnityDi.Services" />
    <assembly name="UnityDi.Repositories" />
    <namespace name="UnityDi.Contracts" />
    <namespace name="UnityDi.Domain" />
    <namespace name="UnityDi.Services" />
    <namespace name="UnityDi.Repositories" />
    <container>
        <register type="Foo.Worker.Interfaces.IIndividualWorker, Foo.Worker" mapTo="Foo.Worker.Workers.IndividualWorker, Foo.Worker"></register>
    </container>
</unity>

当在一个类或控制器需要IIndividualWorker,我与依赖属性添加它:

When IIndividualWorker is required in a class or controller , I add it with the Dependency attribute :

[Dependency]
public IIndividualWorker IndividualWorker { get; set; }

到目前为止,这工作得很好......

So far, this works fine......

我已经打了一个问题,现在我不明白,我承认我的团结知识不是最深的。

I've hit a problem now that I don't understand, and I admit my Unity knowledge isn't the deepest.

我创建了一个新的MVC Web应用程序,它会利用相同的后端层作为第一个应用,如上图所示。我已经配置以同样的方式的Web.Config中,所有的参考文献都到位。我得到的问题是,新的Web应用程序使用ASP.Net身份为它的安全机制(previous应用了简单的会员)。

I created a new MVC Web Application, it will utilize the same back-end tiers as the first application, shown above. I have configured the Web.Config in the same way, and all the references are in place. The problem I get is that the new web application utilizes ASP.Net Identity for it's security mechanism (the previous application had Simple Membership).

在注册一个新用户,使用标准的ASP.Net MVC认证模板,我得到的错误当前类型,Microsoft.AspNet.Identity.IUserStore`1 [Foo.Web2.Models.ApplicationUser]在接口并不能构成。是否缺少类型映射?

On registering a new User, using the standard ASP.Net MVC Authentication templates, I get the error "The current type, Microsoft.AspNet.Identity.IUserStore`1[Foo.Web2.Models.ApplicationUser], is an interface and cannot be constructed. Are you missing a type mapping?"

看来团结就是试图处理ApplicationUserManager类的参数

It seems that Unity is trying to handle the parameter of the ApplicationUserManager class

public ApplicationUserManager(IUserStore<ApplicationUser> store)

有没有办法解决?我能得到统一,离开这个(和presumably其他模板code)独自一人,只有设法解决这些我已经在我的定义.CONFIG?

Is there a way around this? Can I get Unity to leave this (and presumably other templated code) alone, and only try to resolve those I've defined in my .Config?

谢谢,

推荐答案

在最后,我明确地增加了IUserStore

In the end, I explicitly added the registration of the IUserStore

container.RegisterType(typeof(IUserStore<ApplicationUser>), typeof(UserStore<ApplicationUser>));

什么扔我是,除了团结,似乎整个codeBase的是previously工作影响很多接口。这种性质的任何更多的错误我一定会明确地注册有问题的接口。

What threw me was that the addition of Unity seems to affect many interfaces throughout the codebase that previously worked. Any more errors of this nature I'll be sure to explicitly register the offending interface.

我仍然怀疑,必须有选择,以保持团结仅限于所提供的注册,可以在任何统一高手指教?

I still suspect there must be options to keep Unity limited to only the registrations provided, can any Unity experts advise?

这篇关于团结集装箱试图解决非注册的类型,抛出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆