i running into this situation. I see many developers here are facing similar scenarios but there are still a couple of details i would like to point out.
I have the typical situation where i have a collection of child objects. I am adding more contacts to an user.contacts property. In my mapping lazy=true on that property. I never leave the scope of the method calling the repository.
I am using Windsor WCF Facility. It was my understanding that, based on my configuration, sessions were going to live either for the life of the request, or the WcfSession.
Ask me any question if you need more info.
Here is my global.asax in my wcf service.
container = new WindsorContainer();
container.AddFacility<AutoTxFacility>();
container.Register(Component.For<INHibernateInstaller>().ImplementedBy<NHibernateInstaller>());
container.AddFacility<NHibernateFacility>();
container.AddFacility<WcfFacility>();
container.Register(
Component.For<IRegistrationService>().ImplementedBy<RegistrationService>().LifestylePerWcfSession(),
Component.For<IAuthService>().ImplementedBy<AuthService>().LifestylePerWcfSession(),
Component.For<IUserRepository>().ImplementedBy<UserRepository>().LifestylePerWcfSession(),
Component.For<IActionWebService>().ImplementedBy<ActionWebService>().Named("ActionWebService").LifestylePerWcfSession(),
Component.For<ISession>().LifeStyle.Singleton.UsingFactoryMethod(x => container.Resolve<ISessionManager>().OpenSession()));
Here is my repository...
public class UserRepository: IUserRepository
{
Func<ISession> session;
public UserRepository(Func<ISession> _session)
{
session = _session;
}
[Transaction]
public void Store(User user)
{
using (var tx = session())
{
tx.SaveOrUpdate(user);
}
}
[Transaction]
public User GetById(int id)
{
using (var tx=session())
{
return tx.Get<User>(id);
}
}
}
Here is my call to the repository
public void AddContactsToUser(int userID, IList<User> contacts)
{
var user = userRepository.GetById(userID);
if (user == null)
throw new Exception("User does not exist");
user.AddReferralToUser(contacts.ToArray());
userRepository.Store(user);
}
This is where i get the error about Session being closed.
Thank you in advance.