温莎城堡,功能NHibernate和自动映射的ISession已解决的问题 [英] Castle Windsor, Fluent Nhibernate, and Automapping Isession closed problem

查看:207
本文介绍了温莎城堡,功能NHibernate和自动映射的ISession已解决的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的整个城堡温莎,NHibernate的,流利并自动映射堆栈所以在这里请原谅我的无知。我不想寄出对这段另外一个问题,因为它似乎已经有一个庞大的试图得到一个解决方案温莎nhib ISession的管理问题的问题数,但到目前为止他们都没有解决我的问题。我仍然得到一个ISession的是,当我试图从我的库到DB调用封闭例外,这里是我的容器设置code。

I'm new to the whole castle Windsor, Nhibernate, Fluent and Automapping stack so excuse my ignorance here. I didn't want to post another question on this as it seems there are already a huge number of questions that try to get a solution the Windsor nhib Isession management problem, but none of them have solved my problem so far. I am still getting a ISession is closed exception when I'm trying to call to the Db from my Repositories,Here is my container setup code.

container.AddFacility<FactorySupportFacility>()
            .Register(
                Component.For<ISessionFactory>()
                    .LifeStyle.Singleton
                    .UsingFactoryMethod(() => Fluently.Configure()
                                                  .Database(
                                                      MsSqlConfiguration.MsSql2005.
                                                          ConnectionString(
                                                              c => c.Database("DbSchema").Server("Server").Username("UserName").Password("password")))
                                                  .Mappings
                                                     (
                                                      m => 
                                                      m.AutoMappings.Add
                                                        (
                                                          AutoMap.AssemblyOf<Message>(cfg)
                                                          .Override<Client>(map =>
                                                          {
                                                              map.HasManyToMany(x => x.SICCodes).Table("SICRefDataToClient");
                                                          })
                                                          .IgnoreBase<BaseEntity>()
                                                          .Conventions.Add(DefaultCascade.SaveUpdate())
                                                          .Conventions.Add(new StringColumnLengthConvention(),new EnumConvention())
                                                          .Conventions.Add(new EnumConvention())

                                                          .Conventions.Add(DefaultLazy.Never())
                                                        )
                                                      )
                                                  .ExposeConfiguration(ConfigureValidator)
                                                  .ExposeConfiguration(BuildDatabase)
                                                  .BuildSessionFactory() as SessionFactoryImpl),
                 Component.For<ISession>().LifeStyle.PerWebRequest.UsingFactoryMethod(kernel => kernel.Resolve<ISessionFactory>().OpenSession()
                                                  ));

在我的仓库我注入私人只读的ISession会议; 和followes使用它

In my repositories i inject private readonly ISession session; and use it as followes

 public User GetUser(int id)
    {
        User u;

            u = session.Get<User>(id);
            if (u != null && u.Id > 0)
            { 
                NHibernateUtil.Initialize(u.UserDocuments);
            }


        return u;

在我的web.config里面&LT;&的HttpModules GT; 。我也开始加入这一行

in my web.config inside <httpModules>. i have also added this line

      <add name="PerRequestLifestyle" 
type="Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule, Castle.Windsor"/>

我我仍然失踪之谜的一部分在这里,我不能相信这是这样的配置对于任何Web应用程序开发NHibernate和温莎城堡的基本需要一个复杂的东西。

I'm i still missing part of the puzzle here, i can't believe that this is such a complex thing to configure for a basic need of any web application development with nHibernate and castle Windsor.

我一直试图在这里<一个按照code href=\"http://stackoverflow.com/questions/2671625/windsor-nhibernate-isession-mvc\">windsor-nhibernate-isession-mvc我贴我的问题有,因为他们似乎有相同的问题,但该矿没有解决。

I have been trying to follow the code here windsor-nhibernate-isession-mvc and i posted my question there as they seemed to have the exact same issue but mine is not resolved.

更新
MooKid8000现在我已经更新了我的城堡寄存器code将该

UPDATE MooKid8000 i have now updated my castle register code to this

private void AddRepositories()
    {
        container.Register(
            AllTypes.FromAssembly(typeof(MembershipRepository).Assembly)
                .Pick()
                .Configure(c => c.Interceptors(
                    InterceptorReference.ForKey("simpleLogger")).Anywhere
                )
                .Configure(c => c.LifeStyle.Is(LifestyleType.Transient))
                .WithService.FirstInterface());
    }

但我仍然得到的ISession被关闭的问题,难道我的服务必须同时注册为短暂,你能否更详细地解释为什么他们应该是短暂的,而不是单身的

But I'm still getting the ISession is closed issue, Do my services need to be registered as Transient as well, Can you explain in more detail why they should be transient and not singleton's

更新
MooKid8000建议是100%正确的,我只是需要确保我的服务和信息库,所有作用域为LifestyleType.Transient这样的Isession没有被消灭。大斑Mookid8000,甚至没有看到我的城堡注册code开始。

UPDATE MooKid8000 suggestion was 100% correct , I just need to make sure my services and repositories where all scoped as LifestyleType.Transient so the ISession wasn't wiped out. Great spot Mookid8000 without even seeing my castle registration code initially.

有关谁是intrested与我联系,我可以给你我的容器设置。

For anybody that is intrested contact me and i can send you my container setup.

推荐答案

你记得有一个短暂的生活方式注册您的仓库?

Did you remember to register your repositories with a transient lifestyle?

如果您的存储库是单身(这是默认的生活方式与温莎),然后注入的Isession 实例会中招,导致闭门会议的错误以后。

If your repositories are singletons (which is the default lifestyle with Windsor), then the injected ISession instance will be "caught", resulting in closed session errors later on.

请注意,使用任何服务,您的资料库也必须有一个短暂的生活方式,任何服务以及使用这些服务,等等。

Note that any services using your repositories must also have a transient lifestyle, as well as any services using those services, and so on.

通常情况下,你可以说,生活方式粒度不应该增加,因为你去从根组成较远,否则某种作用域会发生。当然,这并不总是一个问题,但它可以导致一些pretty难以诊断的错误。

Generally, you could say that the lifestyle granularity should not increase as you go further away from the composition root, otherwise some kind of scoping will happen. Of course this is not always a problem, but it can lead to errors that are pretty hard to diagnose.

这篇关于温莎城堡,功能NHibernate和自动映射的ISession已解决的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆