如何向AccountController添加服务,实现Asp Identity和UnitOf工作模式

时间:2015-10-14 07:08:37

标签: c# asp.net asp.net-mvc entity-framework asp.net-identity

我在AccountController中实现ASP.Net Identity注入服务模式的问题。这是我的代码:

帐户控制器构造函数

public class AccountController : Controller
{
    private readonly IMyProfileService _profileService;
    private readonly IUnitOfWorkAsync _unitOfWorkAsync;
    private ApplicationUserManager _userManager;
    public ApplicationUserManager UserManager
    {
        get
        {
            return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
        }
        private set
        {
            _userManager = value;
        }
    }

    private ApplicationSignInManager _signInManager;
    public ApplicationSignInManager SignInManager
    {
        get
        {
            return _signInManager ?? HttpContext.GetOwinContext().Get<ApplicationSignInManager>();
        }
        private set { _signInManager = value; }
    }

    private ApplicationRoleManager _roleManager;
    public ApplicationRoleManager RoleManager
    {
        get
        {
            return _roleManager ?? HttpContext.GetOwinContext().Get<ApplicationRoleManager>();
        }
        private set
        {
            _roleManager = value;
        }
    }
    public AccountController()
    {      
    }

    public AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager, 
        ApplicationRoleManager roleManager,IMyProfileService profileService ,IUnitOfWorkAsync unitOfWorkAsync )
    {        
        UserManager = userManager;
        SignInManager = signInManager;
        RoleManager = roleManager;
        _profileService = profileService;
        _unitOfWorkAsync = unitOfWorkAsync;
    }

但是当我尝试在任何方法上与这样的服务类进行通信时:

_profileService.GerProfilesByUsername(userName);

它给了我Null Ref Exception ..顺便说一句,这是我使用Unity的IOC分辨率:

   container
            .RegisterType<IDataContextAsync, NovusTNContext>(new PerRequestLifetimeManager())
            .RegisterType<IUnitOfWorkAsync, UnitOfWork>(new PerRequestLifetimeManager())
            .RegisterType<IRepositoryAsync<Education>, Repository<Education>>()
            .RegisterType<IRepositoryAsync<Profile>, Repository<Profile>>()
             .RegisterType<IRepositoryAsync<Job>, Repository<Job>>()
            .RegisterType<IEducationService, EducationService>()
            .RegisterType<IJobService, JobService>()
            .RegisterType<IMyProfileService, MyProfileService>()

            //*****************Membership Authentication Registration***************//

            .RegisterType<DbContext, MembershipContext>(new HierarchicalLifetimeManager())
            .RegisterType<UserManager<ApplicationUser>>(new HierarchicalLifetimeManager())
            .RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>(new HierarchicalLifetimeManager())
            .RegisterType<AccountController>(new InjectionConstructor());

我使用此功能的目的是当用户注册或登录时,我可以使用配置文件服务在注册时创建新的配置文件并在登录时提取配置文件。任何帮助都会受到重视。 TIA

0 个答案:

没有答案