Ninject binding to constant value error in MVC3 with RavenDB session

I have seen many different ways to configure Ninject using ASP.NET MVC, but the implementation seems to change slightly with each version of the MVC framework. I am trying to inject a RavenDB session into my repository. Here is what I have that almost works.

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 enable IDocumentSession?

+3
source share
1 answer

ToConstant(MvcApplication.CurrentSession)evaluated at application startup. What you want is an evaluation delayToMethod(ctx => MvcApplication.CurrentSession)

+14
source

Source: https://habr.com/ru/post/1783795/


All Articles