开发者

How to inject a service with Ninjects into a selfmade RoleProvider class?

While implementing the Roleprovider so that it gets the roles from a database. I keep getting a object no instance of... exception.

As it turns out ninject does not inject my service .

I tried putting the attribuut on top of the property no succes. I tried adding a constructor but then im getting a yellow screen of death telling me there should be a parameterless constructor

The code

Public Class AnipRolProvider
    Inherits RoleProvider
'this service needs to get initialized
    Private _memberhip As IMemberschipService

    Sub New()
        'only this constructor is called by asp by default
    End Sub

    Sub New(memberschipservice As IMemberschipService)
        'this constructor should be called but how ?
        _memberhip = memberschipservice
    End Sub

the only method i need

Public Overrides Function GetRolesForUser(username As String) As String()
        If String.IsNullOrEmpty(username) Then
            Throw New ArgumentException("username is nothing or empty.", "username")
        End If

        Return _memberhip.开发者_Go百科GetRolesForUser(username)

    End Function

How do i implement ninjects so that role provider team up with the ninja?

Additional information :

<roleManager enabled="true" defaultProvider="AnipRoleProvider">
      <providers>
        <clear/>
        <add name="AnipRoleProvider" type="Anip.Core.AnipRolProvider"  />
      </providers>
    </roleManager>

in my web.config there is a reference to aniproleprovider.

offtopic-sidenote : while copying these snipsets i should learn to write better names!


You have to register your IMembershipService in the global.ascx, don't know how to do this in VB put in c# it looks like this :

kernel.Bind<IMembershipService>().To<MembershipService>().InRequestScope();

using System;
using System.Collections.Generic;
using System.Security.Principal;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using Domain.Interfaces;
using Domain.Models;
using Domain.Storage;
using Domain.Services;
using Ninject;
using Ninject.Syntax;
using WebApp.Controllers;
using WebApp.Mailers;
using WebApp.ModelBinders;

namespace WebApp
{

    public class MvcApplication : NinjectHttpApplication
    {
    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new HandleErrorAttribute());
    }

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );

    }

    protected override IKernel CreateKernel()
    {
        var kernel = new StandardKernel();
        kernel.Load(Assembly.GetExecutingAssembly());

        kernel.Bind<ISession>().To<MongoSession>().InRequestScope();
        kernel.Bind<IAuthenticationService>().To<AuthenticationService>().InRequestScope();
        kernel.Bind<IMailer>().To<Mailer>().InRequestScope();
        kernel.Bind<IFileProvider>().To<MongoFileProvider>().InRequestScope();

        return kernel;
    }

    protected override void OnApplicationStarted()
    {
        base.OnApplicationStarted();

        AreaRegistration.RegisterAllAreas();
        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);
    }

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜