using RavenDB with ServiceStack

前端 未结 4 1159
梦毁少年i
梦毁少年i 2021-02-06 13:14

I read this post by Phillip Haydon about how to use NHibernate/RavenDB with ServiceStack.
I don\'t see the point about getting the IDocumentStore and open new session every

4条回答
  •  我寻月下人不归
    2021-02-06 13:38

    I tried the answer given by Felipe Leusin but it has not worked for me. The main thing that I want to achieve is having a single DocumentSession.SaveChanges call per request. After looking at the RacoonBlog DocumentSession lifecycle management and at ServiceStack request lifecycle events I put together a configuration that works for me:

        public override void Configure(Funq.Container container)
        {
            RequestFilters.Add((httpReq, httpRes, requestDto) =>
                {
    
                    IDocumentSession documentSession = Container.Resolve().OpenSession();
                    Container.Register(documentSession);
                });
    
            ResponseFilters.Add((httpReq, httpRes, requestDto) =>
                {
                    using (var documentSession = Container.Resolve())
                    {
                        if (documentSession == null)
                            return;
    
                        if (httpRes.StatusCode >= 400 && httpRes.StatusCode < 600)
                            return;
    
                        documentSession.SaveChanges();
                    }
                });
            var documentStore = new DocumentStore
                {
                    ConnectionStringName = "RavenDBServer",
                    DefaultDatabase = "MyDatabase",
                }.Initialize();
    
            container.Register(documentStore);
    

提交回复
热议问题