I Have the following ninject configuration

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

And my RavenDB Store Configuration

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

Session Context Management

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

And then I have Service Classes

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

   }    

My question is this

The call to createSession in Ninject configuration. Does it get called at the beginning of every request or it is called once during the Application Startup and resulting instance is injected upon every request

Do the 2 service implementations receive the same instance of the session object?

有帮助吗?

解决方案

InRequestScope bound objects are created once per request. If you use both services in the same request they will get the same instance.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top