مشكلة تسعة من المشكلات المرتبطة بالقيمة الثابتة في MVC3 مع جلسة RavendB

StackOverflow https://stackoverflow.com/questions/4611832

  •  26-09-2019
  •  | 
  •  

سؤال

لقد رأيت الكثير من الطرق المختلفة لتكوين Ninject مع ASP.NET MVC ، ولكن يبدو أن التنفيذ يتغير قليلاً مع كل إصدار من إطار عمل MVC. أحاول حقن جلسة Ravendb في مستودعي. إليكم ما لدي تقريبًا.

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

عندما تحاول حل Idocumentesseess ، أحصل على الخطأ التالي.

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

أي أفكار حول كيفية جعل idocumentess حل؟

هل كانت مفيدة؟

المحلول

ToConstant(MvcApplication.CurrentSession) يتم تقييمه عند بدء التطبيق. ما تريده هو تأخير التقييم ToMethod(ctx => MvcApplication.CurrentSession)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top