Using Ninject with Membership.Provider

我怕爱的太早我们不能终老 提交于 2019-12-01 03:24:06

This is how it should be done today with new versions of both MVC and Ninject (version 3):

You have access to the DependencyResolver instance and Ninject sets itself as the current DependencyResolver. That way you don't need hacks to get access to the static Ninject kernel. Please note, my example uses my own IUserService repository for Membership...

IUserService _userService = DependencyResolver.Current.GetService<IUserService>();

The best solution I found was the following:

private IRepository _repository;

[Inject]
public IRepository Repository
{
    get { return _repository; }
    set { _repository= value; }
}

public CustomMembershipProvider()
{
    NinjectHelper.Kernel.Inject(this);
}

Where NinjectHelper is a static helper class to get the Kernal from.

Ian Davis

Since the membership collection and the Membership.Provider instance are created before Ninject can instantiate them, you need to perform post creation activation on the object. If you mark your dependencies with [Inject] for your properties in your provider class, you can call kernel.Inject(Membership.Provider) - this will assign all dependencies to your properties.

Eugene Gluhotorenko

I haven't used Ninject ever. but in StructureMap i set this dependency:

expression.For<MembershipProvider>().Add(System.Web.Security.Membership.Provider);

and it works fine.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!