Ninject problem binding to constant value in MVC3 with a RavenDB session

Posted by Jim on Stack Overflow See other posts from Stack Overflow or by Jim
Published on 2011-01-06T04:45:49Z Indexed on 2011/01/06 4:54 UTC
Read the original article Hit count: 305

Filed under:
|
|

I've seen a lot of different ways of configuring Ninject with ASP.NET MVC, but the implementation seems to change slightly with each release of the MVC framework. I'm trying to inject a RavenDB session into my repository. Here is what I have that's almost working.

public class MvcApplication : NinjectHttpApplication
{
    ...

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

        AreaRegistration.RegisterAllAreas();

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

    protected override IKernel CreateKernel()
    {
        return new StandardKernel(new MyNinjectModule());
    }

    public static IDocumentSession CurrentSession
    {
        get { return (IDocumentSession)HttpContext.Current.Items[RavenSessionKey]; }
    }
    ...
}

public class MyNinjectModule : NinjectModule
{
    public override void Load()
    {
        Bind<IUserRepository>().To<UserRepository>();
        Bind<IDocumentSession>().ToConstant(MvcApplication.CurrentSession);
    }
}

When it tries to resolve IDocumentSession, I get the following error.

Error activating IDocumentSession using binding from IDocumentSession to constant value
Provider returned null.
Activation path:
  3) Injection of dependency IDocumentSession into parameter documentSession of constructor of type UserRepository

Any ideas on how to make the IDocumentSession resolve?

© Stack Overflow or respective owner

Related posts about mvc

Related posts about ninject