質問

私は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の決意を作る方法上の任意のアイデア?

役に立ちましたか?

解決

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

遅延評価であります
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top