Domanda

Ho visto un sacco di modi diversi di configurare Ninject con ASP.NET MVC, ma l'implementazione sembra cambiare leggermente con ogni versione del framework MVC. Sto cercando di iniettare una sessione di RavenDB nel mio repository. Ecco quello che ho che è quasi di lavoro.

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

Quando si tenta di risolvere IDocumentSession, ottengo il seguente errore.

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

Tutte le idee su come rendere la determinazione IDocumentSession?

È stato utile?

Soluzione

ToConstant(MvcApplication.CurrentSession) viene valutata all'avvio dell'applicazione. Quello che vuoi è ritardata ToMethod(ctx => MvcApplication.CurrentSession) valutazione

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top