身份提供者和Unity依赖注入 [英] Identity Provider and Unity Dependency Injection

查看:215
本文介绍了身份提供者和Unity依赖注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经下载了此示例中,我可以尝试在ASP.NET MVC 5身份提供者的特点:

<一个href=\"http://www.nuget.org/packages/Microsoft.AspNet.Identity.Samples\">http://www.nuget.org/packages/Microsoft.AspNet.Identity.Samples

我已经多次使用统一DI为我的仓库,服务工作和其他东西的单位没有问题。

但现在,当我安装统一DI,我有一个使用身份提供程序,接口和DI很多问题

我刚刚下载的例子的code和我已经配置了统一DI,但我不希望使用统一的标识成员,我想用统一DI只是对我的东西(IRepository, IService,IUnitOfWork等)

我有这样的错误,当我尝试注册一个用户:


  

当前类型,
  Microsoft.AspNet.Identity.IUserStore`1 [Ecoavantis.Interactive.GCI.Models.ApplicationUser]
  是一个接口,并且不能构成。是否缺少类型
  映射?


我看了一些帖子中,他们说我必须包括这样的事情,但我并不需要注入的身份提供商的依赖...

  container.RegisterType&LT;&的UserManager LT; ApplicationUser&GT;&GT;(新HierarchicalLifetimeManager());
            container.RegisterType&所述; IUserStore&下; ApplicationUser&gt;中UserStore&所述; ApplicationUser&GT;&GT;(新HierarchicalLifetimeManager());

谁能帮我吗?

code示例:

  ///&LT;总结&gt;注册类型映射与Unity容器&LT; /总结&gt;
        ///&LT; PARAM NAME =容器&gt;在团结容器配置&LT; /参数&GT;
        ///&LT;&言论GT;有没有需要登记的具体类型,如控制器或API控制器(除非你想
        ///更改默认设置),因为允许统一解决一个具体的类型,即使它不是LT pviously注册$ P $&; /备注&GT;
        公共静态无效RegisterTypes(IUnityContainer容器)
        {
            //注:要从web.config中加载取消下面的行。确保一个Microsoft.P​​ractices.Unity.Configuration添加到using语句。
            // container.LoadConfiguration();
            // TODO:在这里注册您的类型
            //container.RegisterType<DbContext,ApplicationDbContext&GT;(新ContainerControlledLifetimeManager());
            container.RegisterType&所述; IUnitOfWorkAsync,的UnitOfWork&GT;(新PerRequestLifetimeManager());
            container.RegisterType&LT; IMainPanelService,MainPanelService&GT;();
            container.RegisterType(typeof运算(IRepositoryAsync&LT;&GT;)的typeof(库&LT;&GT;));
            container.RegisterType&所述; IDataContextAsync,ecoavantisinteractivegciContext&GT;(新PerRequestLifetimeManager());
         }


解决方案

好吧,我已经解决了我的问题,我已经注射了这种方法的相关性,但我不很明白为什么它的工作...

现在,身份正常工作与我的团结DI

  container.RegisterType&LT;&的AccountController GT;(新InjectionConstructor());
container.RegisterType&所述; RolesAdminController&GT;(新InjectionConstructor());
container.RegisterType&所述; ManageController&GT;(新InjectionConstructor());
container.RegisterType&所述; UsersAdminController&GT;(新InjectionConstructor());

I have downloaded this sample in which I can try the features of Identity Provider in ASP.NET MVC 5:

http://www.nuget.org/packages/Microsoft.AspNet.Identity.Samples

I have used many times Unity DI for my repositories, services, unit of work and other stuff without problems.

But now, when I install Unity DI, I have many problems with the Interfaces and DI that uses the Identity Provider.

I just have downloaded the code of the example and I have configured the Unity DI, but I don't want to use Unity for the Identity Membership, I want to use Unity DI just for me stuff (IRepository, IService, IUnitOfWork, etc.)

I have this error when I try to register a user:

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

I read some post in which they said that I must include something like this, but I don't need to inject dependencies in Identity Provider...

container.RegisterType<UserManager<ApplicationUser>>(new HierarchicalLifetimeManager());
            container.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>(new HierarchicalLifetimeManager());

Can anyone help me please?

Code Example:

 /// <summary>Registers the type mappings with the Unity container.</summary>
        /// <param name="container">The unity container to configure.</param>
        /// <remarks>There is no need to register concrete types such as controllers or API controllers (unless you want to 
        /// change the defaults), as Unity allows resolving a concrete type even if it was not previously registered.</remarks>
        public static void RegisterTypes(IUnityContainer container)
        {
            // NOTE: To load from web.config uncomment the line below. Make sure to add a Microsoft.Practices.Unity.Configuration to the using statements.
            // container.LoadConfiguration();
            // TODO: Register your types here


            //container.RegisterType<DbContext, ApplicationDbContext>(new ContainerControlledLifetimeManager());
            container.RegisterType<IUnitOfWorkAsync, UnitOfWork>(new PerRequestLifetimeManager());
            container.RegisterType<IMainPanelService, MainPanelService>();
            container.RegisterType(typeof (IRepositoryAsync<>), typeof (Repository<>));
            container.RegisterType<IDataContextAsync, ecoavantisinteractivegciContext>(new PerRequestLifetimeManager());
         }

解决方案

Ok, I have resolved my problem, I have injected the dependencies with this method, but I don't understand very well why it's work...

Now, Identity works fine with my Unity DI

container.RegisterType<AccountController>(new InjectionConstructor());
container.RegisterType<RolesAdminController>(new InjectionConstructor());
container.RegisterType<ManageController>(new InjectionConstructor());
container.RegisterType<UsersAdminController>(new InjectionConstructor());

这篇关于身份提供者和Unity依赖注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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