3

ASP.NET MVCを使用してNinjectを構成するさまざまな方法を見てきましたが、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);
    }
}

IDocumentSessionを解決しようとすると、次のエラーが発生します。

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

IDocumentSessionを解決する方法について何かアイデアはありますか?

4

1 に答える 1

14

ToConstant(MvcApplication.CurrentSession)アプリケーションの開始時に評価されます。欲しいのは評価の遅れですToMethod(ctx => MvcApplication.CurrentSession)

于 2011-01-06T15:39:46.897 に答える