Skip to content

Problem with Dependency Injection #15

Description

@Tabgyn

I'm trying to add dependency injection to this project with AutoFac.

I made the following changes

Startup.cs

public void Configuration(IAppBuilder app)
{
    var builder = new ContainerBuilder();

    builder.RegisterControllers(typeof(MvcApplication).Assembly);
    builder.RegisterType<ApplicationDbContext>().AsSelf().InstancePerRequest();
    builder.RegisterType<ApplicationUserStore>().As<IUserStore<User, long>>().InstancePerRequest();
    builder.RegisterType<ApplicationUserManager>().AsSelf().InstancePerRequest();
    builder.RegisterType<ApplicationRoleStore>().As<IRoleStore<Role, long>>().InstancePerRequest();
    builder.RegisterType<ApplicationRoleManager>().AsSelf().InstancePerRequest();
    builder.RegisterType<ApplicationGroupStore>().AsSelf().InstancePerRequest();
    builder.RegisterType<ApplicationGroupManager>().AsSelf().InstancePerRequest();
    builder.RegisterType<ApplicationSignInManager>().AsSelf().InstancePerRequest();
    builder.Register(c => HttpContext.Current.GetOwinContext().Authentication).InstancePerRequest();
    builder.Register(c => app.GetDataProtectionProvider()).InstancePerRequest();

    var container = builder.Build();

    DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

    app.UseAutofacMiddleware(container);
    app.UseAutofacMvc();

    ConfigureAuth(app);
}

Startup.Auth.cs

public void ConfigureAuth(IAppBuilder app)
{
    // Configure the db context, user manager and signin manager to use a single instance per request
    //app.CreatePerOwinContext(ApplicationDbContext.Create);
    //app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
    //app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);
    //app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);

    ...
}

IdentityConfig.cs

public ApplicationUserManager(IUserStore<User, long> store, IDataProtectionProvider dataProtectionProvider)
            : base(store)
{
    UserValidator = new UserValidator<User, long>(this)
    {
        AllowOnlyAlphanumericUserNames = false,
        RequireUniqueEmail = true
    };

    PasswordValidator = new PasswordValidator
    {
        RequiredLength = 6,
        RequireNonLetterOrDigit = true,
        RequireDigit = true,
        RequireLowercase = true,
        RequireUppercase = true,
    };

    UserLockoutEnabledByDefault = true;
    DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5);
    MaxFailedAccessAttemptsBeforeLockout = 5;
    EmailService = new EmailService();
    UserTokenProvider = new DataProtectorTokenProvider<User, long>(dataProtectionProvider.Create("ASP.NET Identity"));
}

The problem is that in the ApplicationGroupManager constructor HttpContext.Current.GetOwinContext (). Get <ApplicationDbContext> () returns null

public ApplicationGroupManager()
{
    _db = HttpContext.Current.GetOwinContext().Get<ApplicationDbContext>();
    _userManager = HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
    _roleManager = HttpContext.Current.GetOwinContext().Get<ApplicationRoleManager>();
    _groupStore = new ApplicationGroupStore(_db);
}

Would anyone know how to solve this?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions