1

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!

David
  • 4,185
  • 2
  • 27
  • 46
  • you should not have a parameterless constructor, can you put the code where you use AnipRolProvider? I don't understand where your yellow screen come for, you register the IMembershipService in global? – VinnyG Apr 10 '11 at 18:23
  • It'd be useful to state what's on the YSOD. Also, can you explain why you think it _should_ be injecting? Just putting the attributes in doesnt have any effect unless the objects are being created by Ninject. (The docs explain this point, just reiterating in case it wasnt clear) – Ruben Bartelink Apr 10 '11 at 22:37
  • Does my answer help you for this question? Do you still have a problem? – VinnyG Apr 16 '11 at 15:02
  • will look into it tonight. bit buzzy with other stuff , my appolize for late reply. – David Apr 18 '11 at 07:14
  • @VinnyG what doest the .InRequestScope() do ? also thanks for the mvcstarter kit will be usefull for next projects – David Apr 18 '11 at 17:12
  • 1
    Basicly it tell Ninject that your object will live in the scope of the request, every request will build a new object. Take a look at this question for more information : http://stackoverflow.com/questions/3348106/when-to-use-singleton-vs-transient-vs-request-using-ninject-and-mongodb – VinnyG Apr 18 '11 at 18:14

1 Answers1

3

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

}
VinnyG
  • 6,883
  • 7
  • 58
  • 76
  • Thanks @Remo, did'nt know that, don't know where I took this code from but I edited my answer, is it ok how I use it now? – VinnyG Apr 11 '11 at 02:10