Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio 2013 MVC template raises errors when used with Unity

I have created a new MVC5 application in Visual Studio 2013 (preview) with the latest ASP.NET and Web Tools 2013 Preview Refresh (1.0.0-beta1). I noticed that the IdentityConfig.cs is gone, together with the user models which are now part of framework. And IdentityStoreManager is introduced together with IdentityAuthenticationManager in default AccountController.

When I build and run the standard project everything works fine! So far so good, yeayh!

Then I install Unity from Nuget. I pull Unity and Unity.MVC (both version 3.0.1304.0). The installation of these packages results in 3 references and 2 project files added (UnityConfig.cs and UnityMVCActivator.cs). I don't touch any files and try to run the web app. The home page displays nicely but when I go to "Register" or "Login" page I get an error. The AccountController which uses the new Identity model throws the following exception (modified):

The type IIdentityStoreContext does not have an accessible constructor.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

[ResolutionFailedException: Resolution of the dependency failed, type = "Teamster.Web.Controllers.AccountController", name = "(none)". Exception occurred while: while resolving.

Exception is: InvalidOperationException - The type IIdentityStoreContext does not have an accessible constructor.

At the time of the exception, the container was:

Resolving Teamster.Web.Controllers.AccountController,(none) Resolving parameter "storeManager" of constructor Teamster.Web.Controllers.AccountController(Microsoft.AspNet.Identity.EntityFramework.IdentityStoreManager storeManager, Microsoft.AspNet.Identity.EntityFramework.IdentityAuthenticationManager authManager) Resolving Microsoft.AspNet.Identity.EntityFramework.IdentityStoreManager,(none) Resolving parameter "context" of constructor Microsoft.AspNet.Identity.EntityFramework.IdentityStoreManager(Microsoft.AspNet.Identity.IIdentityStoreContext context) Resolving Microsoft.AspNet.Identity.IIdentityStoreContext,(none) ]

AccountController have a perameterless constructor, but as the exception sais the IIdentityStoreContext used in AccountControllers constructor is the problem for unity. IIdentityStoreContext is in the Identity framework.

How can I fix this, is it possible to resolve this by configuring the Unity container in some way?

For clearness, heres the default AccountController ctors.:

public AccountController() 
    {
        IdentityStore = new IdentityStoreManager();
        AuthenticationManager = new IdentityAuthenticationManager(IdentityStore);
    }

    public AccountController(IdentityStoreManager storeManager, IdentityAuthenticationManager authManager)
    {
        IdentityStore = storeManager;
        AuthenticationManager = authManager;
    }
like image 791
Howie Avatar asked Jul 16 '13 22:07

Howie


1 Answers

A big thank you! to imran_ku07 @ ASP.NET who solved my issue.

I had totally forgotten that Unity picks the constructor with the most parameters by default if I don't specify otherwhise. (Unity Framework IoC with default constructor)

I registered the following in Unityconfig and it solved my problem:

container.RegisterType<AccountController>(new InjectionConstructor());
like image 161
Howie Avatar answered Nov 15 '22 03:11

Howie