Unity container trying to resolve unregistered type, throwing error

I inherited an existing ASP.Net MVC project that uses the Unity DI container. All its registrations are defined in the web.config.

When a new project / class is injected into a project, I create it along with the frontend 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>

      

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

[Dependency]
public IIndividualWorker IndividualWorker { getset; }

      

So far this works great ...

Now I am facing a problem, but I do not understand and I admit that my knowledge of Unity is not the deepest.

I created a new MVC web application, it will use the same basic levels as the first application shown above. I configured Web.Config the same way and all links are in place. The problem is that the new web application uses an ASP.Net identity for the security mechanism (the previous application had a simple membership).

When registering a new user using the standard ASP.Net MVC authentication patterns, I get the error " Current type: Microsoft.AspNet.Identity.IUserStore`1 [Foo.Web2.Models.ApplicationUser] is an interface and cannot be constructed. Are you missing a type mapping? "

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

public ApplicationUserManager(IUserStore<ApplicationUser> store)

      

Is there a way to get around this? Can I get Unity to leave this (and presumably other boilerplate code) alone, and try to resolve the ones I defined in my .Config?

Thank,

+1


source to share


1 answer


At the end, I explicitly added the IUserStore registration

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

      



What dumped me is that the addition of Unity seems to affect many of the interfaces of the entire codebase that previously worked. For any errors of this type, I will definitely explicitly log the offending interface.

I still suspect there should be options to allow Unity to be limited to only granted registrations, can any Unity experts advise?

+4


source







All Articles