0

I have DI set up in my project using Ninject. Here is an example for the UserController.

private readonly IRoleRepository roleRepository;
private readonly IUserRepository userRepository;

public UserController(IUserRepository userRepository, IRoleRepository roleRepository)
{
    this.roleRepository = roleRepository;
    this.userRepository = userRepository;
}

In my NinjectWebCommon.cs file I have this:

private static void RegisterServices(IKernel kernel)
{
    kernel.Bind<IControllerFactory>().To<DefaultControllerFactory>();
    kernel.Bind<IUnitOfWork>().To<PortalContext>().InSingletonScope();
    kernel.Bind<IUserRepository>().To<UserRepository>();
    kernel.Bind<IRoleRepository>().To<RoleRepositoroy>();
}    

However, I can't seem to figure out how to inject the user and role repositories into the custom membership provider. Any help is much appreciated as I have hit a wall here. I am using Asp.net MVC4 and EF4 with the repository and unit of work patterns. Thanks.

BBauer42
  • 3,549
  • 10
  • 44
  • 81
  • I struggled with this for a while, I found a solution and posted it to [this related question](http://stackoverflow.com/questions/10053289/ninject-injecting-membership-provider-in-registerservices-of-ninject-initializa/). – mmacneil007 Jul 08 '12 at 14:52

3 Answers3

0

You can use property injection

[Inject]
public IUserRepository userRepository {get;set;};

You will have to be careful regarding the scope of your DBContext though, as the MembershipProvider class is a singleton whose scope is the lifetime of the application.

Jason Kulatunga
  • 5,814
  • 1
  • 26
  • 50
  • When I do that my UserRepository is always null. I have not created a constructor in MyMembershipProvider. I also have nothing in RegisterServices related to the membership provider. – BBauer42 Jun 08 '12 at 15:15
0

As a simple solution you could just property inject them with Ninject.

[Inject]
public IUserRepository UserRepository {get;set;}
Richard Forrest
  • 3,567
  • 2
  • 23
  • 32
  • When I do that my UserRepository is always null? – BBauer42 Jun 08 '12 at 15:14
  • @BBauer42 Sorry difficult to say without seeing what your code is this resolves for me. Are you using public properties wont work otherwise. – Richard Forrest Jun 08 '12 at 15:17
  • @BBauer42 odd. IS the provider in another project to your MVC application? – Richard Forrest Jun 08 '12 at 15:30
  • I add these two lines to the register services method: kernel.Inject(Membership.Provider); kernel.Inject(Roles.Provider); ...then I run again and get an error "This method cannot be called during the application's pre-start initialization stage. " ..I tried adding the two keys to the web config after looking at this thread and it didn't resolve the issue: http://stackoverflow.com/questions/4626647/asp-net-this-method-cannot-be-called-during-the-applications-pre-start-initial – BBauer42 Jun 08 '12 at 15:32
  • provider is in the same project – BBauer42 Jun 08 '12 at 15:33
  • 1
    I was able to do this using DependencyResolver.Current.GetService() in the membership's constructor – BBauer42 Jul 10 '12 at 13:23
0

@feanz, regarding your question:

@BBauer42 odd. IS the provider in another project to your MVC application?

What would you have to do different if the custom provider is in a separate project (class library)?

HackITMngr
  • 263
  • 3
  • 11