Dependency Injection with unity in default MVC bre

2019-09-17 02:16发布

I'm writing a web app and I have used the default Microsoft MVC site as a starting point. After this I created a database of recipes to use in my web app using entity framework and have written a repository and some business layer methods. I then used dependency injection with Unity to remove the coupling between them. I used this code placed in the MvcApplication class in global.asax.cs.

private IUnityContainer Container;
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            ConfigureObjects();


        }

        private void ConfigureObjects()
        {
            Container = new UnityContainer();
            Container.RegisterType<IRecipeRequest, BasicRecipeRequest>();
            Container.RegisterType<IRecipeRepository, RecipeRepositoryEntityFramework>();
            Container.RegisterType<IRecipeContext, RecipeContext>();

            DependencyResolver.SetResolver(new UnityDependencyResolver(Container));
        } 

The bits that are mine are only the bits relating to dependency injection.

After doing this using any page related to user logins would return and error e.g sign up, log in. It would return an error titled this:

The current type, Microsoft.AspNet.Identity.IUserStore`1[UKHO.Recipes.Www.Models.ApplicationUser], is an interface and cannot be constructed. Are you missing a type mapping? 

Looking at the exception thrown in visual studio using diagnostic tools I got this:

"An error occurred when trying to create a controller of type 'UKHO.WeeklyRecipes.Www.Controllers.AccountController'. Make sure that the controller has a parameterless public constructor."

AccountsContoler is a controller created by deafult which i have not touched and it contains a parameterless constructor and another constructor. They look like this:

   public AccountController()
            {
            }


   public AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager )
            {
                UserManager = userManager;
                SignInManager = signInManager;
            }

By putting [InjectionConstructor()] in front of the parameterless constructor the error went away. It seems to me that Unity is trying to resolve the ApplicationUserManager and ApplicationSignInManager even though I have not registered these types and that putting [InjectionConstructor()] made unity see the empty constructor so do nothing. I mainly want to why this has happened as I was under the impression that unity should only interfere with types you have registered. Butter solutions are also welcome.

Edit: This also happens when you wish to change an account setting but instead errors with the ManageContoler this can also be solved by putting [InjectionConstructor()] in front of the empty constructor.

1条回答
Juvenile、少年°
2楼-- · 2019-09-17 02:35

You configured the dependencies only of the following objects:

Container.RegisterType<IRecipeRequest, BasicRecipeRequest>();
            Container.RegisterType<IRecipeRepository, RecipeRepositoryEntityFramework>();
            Container.RegisterType<IRecipeContext, RecipeContext>();

But on your controller, you have 2 dependencies that are not configured, ApplicationUserManager and ApplicationSignInManager .

Unity doesnt know about these dependencies, so it can't inject on the constructor, thus trying to invoke the parameterless constructor.

If you have a constructor on your controller with parameters, unity will look for it and try to resolve all the dependencies, no matter which ones your configured. If it doenst find an object, it breaks.

查看更多
登录 后发表回答