title: Using Ninject with a Custom Role provider in an MVC3 app
body: <p>I'm trying to use a custom role provider in an MVC3 app.  I've already got the membership provider working ok using Ninject but can't seem to get the role provider working.  The Membership provider doesn't require a parameterless constructor but role provider does.  Here's some code snippets:</p>
  
  <p><strong>Web.config</strong></p>
  
  <p>
        
      </p>
  
  <pre><code>&lt;membership&gt;
    &lt;providers&gt;
      &lt;clear/&gt;
      &lt;add name="MyMembershipProvider" type="MyApp.Models.NHibernateMembershipProvider" 
           applicationName="myApp" /&gt;
    &lt;/providers&gt;
  &lt;/membership&gt;
  
  &lt;roleManager enabled="true"&gt;
    &lt;providers&gt;
      &lt;add name="MyRolesProvider" type="MyApp.Models.NHibernateRoleProvider"
           applicationName="myApp" /&gt;
    &lt;/providers&gt;
  &lt;/roleManager&gt;
  </code></pre>
  
  <p>I have a Ninject module.</p>
  
  <pre><code>public class MyNinjectModule : NinjectModule
  {
      public override void Load()
      {
          this.Bind&lt;ISession&gt;().ToMethod(
              x =&gt; MyApp.MvcApplication.SessionFactoryData.GetCurrentSession());
  
          // Respository
          this.Bind&lt;IUserRepository&gt;().To&lt;UserRepository&gt;();
          this.Bind&lt;MembershipProvider&gt;().To&lt;NHibernateMembershipProvider&gt;();
          this.Bind&lt;RoleProvider&gt;().To&lt;NHibernateRoleProvider&gt;();
      }
  }
  </code></pre>
  
  <p>The custom Membership provider</p>
  
  <pre><code>public class NHibernateMembershipProvider : MembershipProvider
  {
      private IUserRepository _repo;
  
      public NHibernateMembershipProvider(IUserRepository repository)
      {
          _repo = repository;
      }
      ...
  </code></pre>
  
  <p>The role provider</p>
  
  <pre><code>public class NHibernateRoleProvider : RoleProvider
  {
  
      private IUserRepository _repo;
  
      public NHibernateRoleProvider(IUserRepository repository)
      {
          _repo = repository;
      }
      ...
  </code></pre>
  
  <p>I then configure my controller to require an authorize</p>
  
  <pre><code>    [Authorize(Roles="Admin")]
      public ActionResult Edit(int? id)
      {
         ...
  </code></pre>
  
  <p>I get this error when starting the app.</p>
  
  <pre><code>Parser Error Message: No parameterless constructor defined for this object.
  
  Source Error: 
  
  
  Line 49:     &lt;roleManager enabled="true"&gt;
  Line 50:       &lt;providers&gt;
  Line 51:         &lt;add name="MyRolesProvider" type="MyApp.Models.NHibernateRoleProvider"
  Line 52:              applicationName="myApp" /&gt;
  Line 53:       &lt;/providers&gt;
  </code></pre>
  
  <p>I can access the users through the membership provider, so the repository is being injected ok, but the roles provider seems to be different.  Why does the role provider require a constructor-less parameter?  Is there a simple way to get the role provider to work with Ninject. Any help appreciated.</p>