如何使用 Ninjects 将服务注入到自制的 RoleProvider 类中?

发布于 2024-10-31 08:08:02 字数 1345 浏览 4 评论 0原文

在实现 Roleprovider 时,以便它从数据库获取角色。我不断得到一个对象,没有...异常的实例。

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

我尝试将属性放在属性之上,但没有成功。 我尝试添加一个构造函数,但随后出现黄屏死机,告诉我应该有一个无参数构造函数

代码是

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

如何实现 ninjects 以便角色提供者与忍者合作?

附加信息:

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

在我的 web.config 中有对aniproleprovider的引用。

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

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.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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魂归处 2024-11-07 08:08:02

您必须在 global.ascx 中注册您的 IMembershipService,不知道如何在 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);
    }

}

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

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文