如何使用Injected将服务注入自制的角色提供程序类?

时间:2011-04-10 17:40:33

标签: asp.net-mvc vb.net dependency-injection ninject roleprovider

实现Roleprovider,以便从数据库中获取角色。我一直得到一个没有......例外的对象。

事实证明,ninject没有注入我的服务。

我尝试将attribuut放在属性的顶部没有成功。 我尝试添加一个构造函数,但后来我得到一个黄色的死亡屏幕,告诉我应该有一个无参数构造函数

代码

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

我需要的唯一方法

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.GetRolesForUser(username)

    End Function

如何实施ninject,以便角色提供者与忍者合作?

其他信息:

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

在我的web.config中有一个对an​​iproleprovider的引用。

offtopic-sidenote:在复制这些snipsets时,我应该学会写出更好的名字!

1 个答案:

答案 0 :(得分:3)

你必须在global.asax中注册你的会员服务,不知道如何在VB中放入c#这样做:它看起来像这样:

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);
    }

}