c# - Microsoft Practice 使用 Unity 寄存器类型

标签 c# dependency-injection asp.net-mvc-5 unity-container

我有几个场景很难映射类型。

场景 1:

 public AccountController()
            : this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())))
        {
        }

        public AccountController(UserManager<ApplicationUser> userManager)
        {
            UserManager = userManager;
        }

类型定义

 public class ApplicationUser : IdentityUser
    {
    }

 // Summary:
    //     Implements IUserStore using EntityFramework where TUser is the entity type
    //     of the user being stored
    //
    // Type parameters:
    //   TUser:
    public class UserStore<TUser> : IUserLoginStore<TUser>, IUserClaimStore<TUser>, IUserRoleStore<TUser>, IUserPasswordStore<TUser>, IUserSecurityStampStore<TUser>, IUserStore<TUser>, IDisposable where TUser : global::Microsoft.AspNet.Identity.EntityFramework.IdentityUser
    {
 ....
  }

public class UserManager<TUser> : IDisposable where TUser : global::Microsoft.AspNet.Identity.IUser
    {
....
  }

尝试映射

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

            container.RegisterType(typeof(IDisposable), typeof(UserManager<>));

错误:无法将“Main.Models.ApplicationUser”类型的对象转换为类型“Microsoft.AspNet.Identity.IUserStore1[Main.Models.ApplicationUser]”。 没有映射,我得到这个错误:当前类型 Microsoft.AspNet.Identity.IUserStore1[Main.Models.ApplicationUser] 是一个接口(interface),无法构造。您是否缺少类型映射?

场景 2:

 public RegisterController(
             IUserDataStorage<HandyGuy> session)
        {
            this.handySession = session;
        }

类型定义

 public class HttpUserDataStorage<T> : IUserDataStorage<T>
  where T : class
    {
        public T Access
        {
            get { return HttpContext.Current.Session[typeof(T).FullName] as T; }
            set { HttpContext.Current.Session[typeof(T).FullName] = value; }
        }
    }

尝试映射:

 container.RegisterType(typeof(IUserDataStorage<>), typeof(HttpUserDataStorage<>));

错误:没有错误。但我总是在 handySession.Access 中有 null

最佳答案

感谢 emodendroket 帮助我解决了 Entity Framework 的下一个映射问题。

但是对于我的场景,我可以用两种方式映射类型

第一个

 container.RegisterType(typeof(UserManager<>),
            new InjectionConstructor(typeof(IUserStore<>)));
            container.RegisterType<Microsoft.AspNet.Identity.IUser>(new InjectionFactory(c => c.Resolve<Microsoft.AspNet.Identity.IUser>()));
            container.RegisterType(typeof(IUserStore<>), typeof(UserStore<>));
            container.RegisterType<IdentityUser, ApplicationUser>(new ContainerControlledLifetimeManager());
            container.RegisterType<DbContext, ApplicationDbContext>(new ContainerControlledLifetimeManager());

第二个

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

关于c# - Microsoft Practice 使用 Unity 寄存器类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23810055/

相关文章:

c# - 如何以编程方式关闭或打开 'Windows Features'

c# - 需要在 C# windows 中连接字符串

javascript - NestJS:动态模块的缺点?

java - 按 Jersey (HK2) 的名称动态查找服务

c# - 上传到 Azure 网站时 SSL 证书不在 X509Store 中

c# - C#代码中的div样式

c# - 在 C# 4.0 中使用依赖注入(inject)时处理异常

c# - 如何正确捕获 ASP.Net MVC 5 项目中 View 甚至 Controller 引发的异常

asp.net - 如何从 ASP.NET Identity 获取用户列表?

jquery - 交换导航栏 div 类的动画 jQuery 方法