ravendb, castle IoC ,Wcf facility - doc session liefstyle

陌路散爱 提交于 2019-12-07 05:26:59

问题


What's the recommended lifestyle for raven doc session and store under a windsor ioc, wcf facility setup hosted in IIS?

I keep seeing this error:

Error TempPathInUse (JET_errTempPathInUse, Temp path already used by another database instance)`

here is my setup:

public class RavenInstaller : IWindsorInstaller
    {
        public void Install(IWindsorContainer container, IConfigurationStore store)
        {
            container.Register(
                Component.For<IDocumentStore>().ImplementedBy<DocumentStore>()
                    .DependsOn(new { connectionStringName = "RavenConnectionString" })
                    .OnCreate(DoInitialisation)
                    .LifeStyle.Singleton,
                Component.For<IDocumentSession>()
                    .UsingFactoryMethod(GetDocumentSesssion)
                    .LifeStyle.Transient
                );

            container.Register(Component.For<IEventSeriesRepository>().ImplementedBy<EventSeriesRepository>().LifeStyle.Transient);
            container.Register(Component.For<IEventInstanceRepository>().ImplementedBy<EventInstanceRepository>().LifeStyle.Transient);
            container.Register(
                Component.For<IProductionCompanyRepository>().ImplementedBy<ProductionCompanyRepository>().LifeStyle.
                    Transient);
        }

        static IDocumentSession GetDocumentSesssion(IKernel kernel)
        {
            var store = kernel.Resolve<IDocumentStore>();
            return store.OpenSession();
        }

        public static void DoInitialisation(IKernel kernel, IDocumentStore store)
        {
            store.Initialize();
            IndexCreation.CreateIndexes(typeof(EventSeries_ByName).Assembly, store);

        }
    }

回答1:


This same question about lifecycle was raised in the Raven forums: https://groups.google.com/forum/#!topic/ravendb/wUgULf3eoCg

Ayende's response was: Singleton for the Document Store, Transient / Web Request for the session.




回答2:


I solved this by doing like this:

container.Register(
    Component
        .For<IRavenSessionFactoryBuilder>()
        .ImplementedBy<RavenSessionFactoryBuilder>()
        .LifeStyle.Singleton
    );

container.Register(
    Component
        .For<IDocumentSession>()
        .UsingFactoryMethod(kernel => 
            kernel.Resolve<IRavenSessionFactoryBuilder>()
               .GetSessionFactory()
               .CreateSession()
        )
        .LifeStyle.Transient
    );

// This is the repository making use of the IDocumentSession
container.Register(
    Component
        .For<IDomainRepository>()
        .ImplementedBy<DomainRepository>()
        .LifeStyle.Transient
    );

And here is the RavenSessionFactoryBuilder

public class RavenSessionFactoryBuilder : IRavenSessionFactoryBuilder
{
    private IRavenSessionFactory ravenSessionFactory;

    public IRavenSessionFactory GetSessionFactory()
    {
        return ravenSessionFactory ?? (ravenSessionFactory = CreateSessionFactory());
    }

    private static IRavenSessionFactory CreateSessionFactory()
    {
        return new RavenSessionFactory(
            new DocumentStore {
                Url = "http://localhost:8080"
            });
    }
}

It works like a charm!



来源:https://stackoverflow.com/questions/5310397/ravendb-castle-ioc-wcf-facility-doc-session-liefstyle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!