Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
631 views
in Technique[技术] by (71.8m points)

asp.net mvc - How to configure Ninject for MVC4 & custom Membership provide?

According to this article description custom-membership-provider-with-repository-injection

I implement the custom Membership provide with inject.

Custom Membership provider

using Ninject;

public class CustomMembershipProvider : MembershipProvider
{
        [Inject]
        public IUserRepository UserRepository { get; set; }

[...]

Custom Role Provider

using Ninject;

public class CustomRoleProvider : RoleProvider
{
      [Inject]
      public IUserRoleRepository UserRoleRepository { get; set; }
[...]

within Web.Config

<membership defaultProvider="CustomsMembershipProvider">
      <providers>
        <clear/>
        <add name="CustomsMembershipProvider" type="namespace.CustomsMembershipProvider"/>
      </providers>
</membership>
<roleManager enabled="true" defaultProvider="customRoleProvider">
      <providers>
        <clear/>
        <add name="customRoleProvider" type="namespace.customRoleProvider"/>
      </providers>
</roleManager>

Now within NinjectWebCommon

private static IKernel CreateKernel()
{
      var kernel = new StandardKernel();
      kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
      kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

      RegisterServices(kernel);
      return kernel;
}

private static void RegisterServices(IKernel kernel)
{
  [...]

  kernel.Bind<IUserRepository>().To<UserRepository>();
  kernel.Bind<IUserRoleRepository>().To<UserRoleRepository>();

  // Inject user & role repository into our custom membership & role providers.
   kernel.Inject(Membership.Provider);
   kernel.Inject(Roles.Provider);
}

when I run application I got error

This method cannot be called during the application's pre-start initialization stage.

from kernel.Inject(Membership.Provider); this line

But If I Kernel setting put within Application_Start

I got bellow Error

Error activating IUserRepository No matching bindings are available, and the type is not self-bindable. Activation path: 2) Injection of dependency IUserRepository into property UserRepository of type CustomMembershipProvider 1) Request for CustomeMembershipProvider

How to solve that. ??

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The result is always null. why? because asp.net has it's own static property for membership.

which is membership.provider. and this instance is not part of instance ninject management.

to workaround it , you need to use kernel.inject . but on the generate aspnetmvc.cs you would see that it's injection on PreApplicationStart event and won't let you.

here is the soloution by cipto that solve the problem for me. add this to your NinjectWebCommon

    [assembly: WebActivator.PreApplicationStartMethod(typeof(TopRankFantasy.App_Start.NinjectMVC3), "Start")]
    [assembly: WebActivator.PostApplicationStartMethod(typeof(TopRankFantasy.App_Start.NinjectMVC3), "RegisterMembership")]
    [assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(TopRankFantasy.App_Start.NinjectMVC3), "Stop")]

    public static void RegisterMembership()
    {
        bootstrapper.Kernel.Inject(Membership.Provider);
    } 

Link to article: Ninject and customMembership asp.net mvc 3


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...