0

我有以下 ninject 配置

 private static IKernel CreateKernel()
    {
        var kernel = new StandardKernel();

        kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
        kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
        ConfigureRavenDB(kernel);
        RegisterServices(kernel);

        var resolver = new NinjectDependencyResolver(kernel);
        GlobalConfiguration.Configuration.DependencyResolver = resolver;
        return kernel;
    }

还有我的 RavenDB 存储配置

 private static IDocumentStore ConfigureRavenDB(IKernel container)
    {

        var store = new DocumentStore
            {
                ConnectionStringName = "SpurroConnection"
            };
        store.Initialize();
        container.Bind<IDocumentStore>().ToConstant(store).InSingletonScope();
        container.Bind<IDocumentSession>().ToMethod(CreateSession).InRequestScope();
        return store;
    }

会话上下文管理

    private static IDocumentSession CreateSession(IContext context)
    {
        var store = context.Kernel.Get<IDocumentStore>();
        if(store != null)
        {
            return store.OpenSession();

        }
        throw new Exception("Unable to Bind the IDocument session for this user request");
    }

然后我有服务类

public class ServiceA
{
  private readonly IDocumentSession _documentSession;
  public ServiceA(IDocumentSession documentSession)
  {
       _documentSession = documentSession;
  }

}    

  public class ServiceB
    {
      private readonly IDocumentSession _documentSession;
      public ServiceB(IDocumentSession documentSession)
      {
       _documentSession = documentSession;
      }

   }    

我的问题是这个

Ninject 配置中对 createSession 的调用。它是在每个请求开始时调用,还是在应用程序启动期间调用一次,结果实例在每个请求时注入

2 个服务实现是否接收到相同的会话对象实例?

4

1 回答 1

1

InRequestScope 绑定对象为每个请求创建一次。如果您在同一个请求中使用这两种服务,它们将获得相同的实例。

于 2013-10-22T13:52:35.300 回答