views:

73

answers:

1

I use this approuch http://www.kevinwilliampang.com/2010/04/06/setting-up-asp-net-mvc-with-fluent-nhibernate-and-structuremap/ for setting up fnh with structuremap but after one request I get the following exception

Session is closed! Object name: 'ISession'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ObjectDisposedException: Session is closed! Object name: 'ISession'.

My repository class looks like this:

public class Repository : IRepository {
    private readonly ISession _session;
    public Repository(ISession session) {
        _session = session;
    }
    public T Get<T>(Expression<Func<T, bool>> predicate) {
        return _session.CreateCriteria(typeof(T)).Add(predicate).UniqueResult<T>();
    }

and I register my repository in structuremap like this:

public class RepositoryRegistry : Registry {
    public RepositoryRegistry() {
        Scan(a => {
            a.AssembliesFromApplicationBaseDirectory();
            a.AddAllTypesOf<IRepository>();
        });
    }
}

How can I prevent the session from being closed?

A: 

Are you registering your ISession the same way they do in the example? It should be HttpContext scoped like so:

      x.For<ISession>()
        .HttpContextScoped()
        .Use(context => context.GetInstance<ISessionFactory>().OpenSession());

The other possibility is that something is getting registered as a singleton (and is holding onto a closed session, rather than being recreated with the current session.

After seeing your question on the StructureMap list: http://groups.google.com/group/structuremap-users/browse_thread/thread/8023e0acc43ceeb3#, I see the problem.

You are injecting your repository into the sitemap, which is a singleton. So you will need to give the SiteMap a new session every request like so:

public class MvcSiteMapProvider : SiteMapProvider { 
     public static IRepository Repository { get; set; }; 
     public MvcSiteMapProvider() { }
} 

protected void Application_BeginRequest() { 
     MvcSiteMapProvider.Repository = ObjectFactory.GetInstance<ISession>();
}
Robin Clowers
yes exactly, the registration of ISession looks exactly like that. The only thing that is registered as a Singleton is ISessionFactory, although it is like in the example. Do you have any idea where I could start searching for an error?
Marcus
Hmm, that is weird, you could look at the output of ObjectFactory.WhatDoIHave() and see if everything looks like it is registered correctly. Something must be scoped wrong, or you are explicitly closing the session somewhere.
Robin Clowers
The error seems to occur in my sitemapprovider. Because the constructor must have an empty default constructor I use ObjectFactory.GetInstance<IRepository>() in the constructor and that seems to be the problem? public class MvcSiteMapProvider : SiteMapProvider { private IRepository _repository; public MvcSiteMapProvider() { _repository = ObjectFactory.GetInstance<IRepository>(); } }
Marcus
This is the output from WhatDoIHave() http://pastebin.com/57dRSR8m
Marcus