I'm urrently working on a MVC project based on StructureMap and NHibernate. It's my first time using StructureMap, so I might be using it wrong, which causes these troubles for me.
In my Application_Start I initialize StructureMap like this:
ObjectFactory.Initialize(x =>
{
x.For<ISessionFactory>()
.Singleton()
.Use(FNHSessionHelper.CreateSessionFactory(ConfigurationManager.ConnectionStrings["PortalConnectionString"].ConnectionString));
x.For<ISession>()
.HttpContextScoped()
.Use(context => context.GetInstance<ISessionFactory>().OpenSession());
});
And I dispose it in Application_EndRequest with:
ObjectFactory.ReleaseAndDisposeAllHttpScopedObjects();
One of my views is populated with a list of ExtraOptionGroup's. This list is retrieved from the database using an ExtraOptionService
private ISession _session = ObjectFactory.GetInstance<ISession>();
public List<ExtraOptionGroup> GetAllGroups()
{
return _session.Linq<ExtraOptionGroup>().ToList();
}
The problem is that an ExtraOptionGroup has a List which I try to loop through in my view, but at this point, I end up getting:
Initializing[Common.Entities.ExtraOptionGroup#1]-failed to lazily initialize a collection of role: Common.Entities.ExtraOptionGroup.ExtraOptions, no session or session was closed
Any clues to what Im doing wrong?
Best Regards, Kenneth