Windsor Castle, Free Nubernath and Automation

I am new to the whole Windsor, Nhibernate, Fluent, and Automapping castle, so excuse my ignorance here. I did not want to post another question on this issue, since there seem to already be a huge number of questions that are trying to solve the Windsor nhib Isession wind control problem, but none of them have yet to resolve my problem. I still get ISession closed exception when I try to call 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()
                                                  ));

In my repositories I embed private readonly ISession session;and use it as follow-up

 public User GetUser(int id)
    {
        User u;

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


        return u;

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

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

, , - nHibernate Windsor.

windsor-nhibernate-isession-mvc, , , , , .

UPDATE MooKid8000

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 . . , , singleton's

UPDATE MooKid8000 100% , , , LifestyleType.Transient, ISession . Mookid8000, .

, , , .

+3
1

?

( ), ISession "", .

, , , , , , ..

, , , , - . , , , .

+5

Source: https://habr.com/ru/post/1782602/


All Articles