Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dependency Injection Structuremap ASP.NET Identity MVC 5

I'm using the new ASP.NET MVC 5 Identity framework for authentication. I've traditionally used StructureMap for dependency injection, but I'm having problems wiring it up to work with the new AccountController.

My AccountController constructors look like this:

    public AccountController()
        : this(new UserManager<OmpUser>(new UserStore<OmpUser>(new OmpDbContext())))
    {    

    }

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

My StructureMap config looks like this:

public static class IoC {
    public static IContainer Initialize() {
        ObjectFactory.Initialize(x =>
                    {
                        x.Scan(scan =>
                                {
                                    scan.TheCallingAssembly();
                                    scan.WithDefaultConventions();
                                });

                        //x.Register<IUserStore<OmpUser>>(() =>
                        //    new UserStore<OmpUser>(new OmpDbContext()));

                        x.For<OMPEntities>().HttpContextScoped();

                    });
        return ObjectFactory.Container;
    }
}

When I run up the project I get the following error:

Activation error occured while trying to get instance of type AccountController, key ""

Any ideas of how to new up a UserManager object for construction injection? I've tried searching around but can't find much guidance out there.

like image 268
Sam Huggill Avatar asked Dec 04 '13 10:12

Sam Huggill


People also ask

What is MVC StructureMap?

The purpose of "StructureMap" is to provide us a framework that we can use to easily "inject" one of the "concrete" classes into the application. "StructureMap" also provides us a simple method to configure which "concrete" class to "inject". The configuration can be done in a simple "xml" file. The "HomeController.

Does .NET framework support dependency injection?

NET supports the dependency injection (DI) software design pattern, which is a technique for achieving Inversion of Control (IoC) between classes and their dependencies. Dependency injection in . NET is a built-in part of the framework, along with configuration, logging, and the options pattern.

Which is the NuGet package referred to use dependency injection in MVC core?

ASP.NET MVC 4 Dependency Injection features. Unity integration using Unity. Mvc3 NuGet Package.


2 Answers

Add following code to your Container initialize method.

x.For<Microsoft.AspNet.Identity.IUserStore<ApplicationUser>>()
.Use<Microsoft.AspNet.Identity.EntityFramework.UserStore<ApplicationUser>>();

x.For<System.Data.Entity.DbContext>().Use(() => new ApplicationDbContext());
like image 167
vinayklin Avatar answered Oct 18 '22 12:10

vinayklin


Adding [DefaultConstructor] Attribute will work.

public class AccountController : Controller
{
    private ApplicationSignInManager _signInManager;
    private ApplicationUserManager _userManager;

    [DefaultConstructor]  //This is the attribute you need to add on the constructor
    public AccountController()
    {
    }
   // Other implementations here..........
}
like image 29
sethu madhav Avatar answered Oct 18 '22 13:10

sethu madhav