默认MVC中具有1的依赖项注入会中断登录 [英] Dependency Injection with unity in default MVC breaks sign in

查看:68
本文介绍了默认MVC中具有1的依赖项注入会中断登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Web应用程序,并且已使用默认的Microsoft MVC网站作为起点。之后,我使用实体框架创建了一个食谱数据库以用于我的Web应用程序,并编写了一个存储库和一些业务层方法。然后,我将依赖注入与Unity一起使用以消除它们之间的耦合。我使用了放置在global.asax.cs的MvcApplication类中的代码。

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? 

看着使用诊断工具在Visual Studio中抛出的异常,我得到了:

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是由deafult创建的一个控制器,我没有接触过,它包含一个无参数的构造函数和另一个构造函数。它们看起来像这样:

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;
            }

通过放入 [InjectionConstructor()] 在无参数构造函数前面的错误消失了。在我看来,即使我没有注册这些类型,Unity仍在尝试解析ApplicationUserManager和ApplicationSignInManager,并且将 [InjectionConstructor()] 统一使之看到空的构造函数,所以没做什么。我主要想知道为什么会发生这种情况,因为我的印象是团结只会干扰您注册的类型。也欢迎使用黄油解决方案。

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.

编辑:当您希望更改帐户设置时也会发生这种情况,但是ManageContoler的错误也可以通过将 [InjectionConstructor()] 在空的构造函数前面。

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.

推荐答案

您仅配置了以下对象的依赖项:

You configured the dependencies only of the following objects:

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

但是在您的控制器上,您有2个未配置的依赖项,ApplicationUserManager和ApplicationSignInManager。

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

Unity不了解这些依赖关系,因此无法注入构造函数,因此尝试调用无参数构造函数。

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

如果您的控制器上有一个带参数的构造函数,那么unity会寻找它并尝试解析所有依赖项,无论您配置了哪个依赖项。如果确实找到对象,它就会中断。

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.

这篇关于默认MVC中具有1的依赖项注入会中断登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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