3

I have an ASP.net MVC2 application that is using NHibernate for data access. On every request, even static file requests (images, javascript), a new session is getting created. So for a single view where I'm returning a list, I'm creating around 15 session that don't load anything.

is there way to only create sessions when they are required?

I'm currently using Castle.Windsor to inject the session into my Controllers.

Is there a way to filter out static file requests?

4 Answers 4

3

It sounds like you need to exclude those paths in your routing:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    // add these lines
    routes.IgnoreRoute("{resource}.jpg");
    routes.IgnoreRoute("{resource}.js");
}

Although if you ask me, a request for a static file shouldn't be instantiating a controller. You might want to take a look at your code and figure out why it's doing that.

Sign up to request clarification or add additional context in comments.

Comments

3

Also I figured it out that this isn't a configuration with NHibernate or MVC. It is that ASP.net development server services every request through ASP.net. IIS will not send static file requests through ASP.net unless configured.

From Here (http://www.asp.net/hosting/tutorials/core-differences-between-iis-and-the-asp-net-development-server-cs)

Another core difference between IIS and the ASP.NET Development Server is how they handle requests for static content. Every request that comes into the ASP.NET Development Server, whether for an ASP.NET page, an image, or a JavaScript file, is processed by the ASP.NET runtime. By default, IIS only invokes the ASP.NET runtime when a request comes in for an ASP.NET resource, such as an ASP.NET web page, a Web Service, and so forth. Requests for static content - images, CSS files, JavaScript files, PDF files, ZIP files, and the like - are retrieved by IIS without the involvement of the ASP.NET runtime.

2 Comments

the trick is to only OpenSession when the Controller has a dependency on ISession. Sounds like you are blindly opening a session inside the global BeginRequest. I'm not a fan of that.
This also happens on the new IIS Express.
1

how are you creating the session, as the .jpg should not ask for an instance of ISession, therefor castle will not create one (from my understanding), could you post the castle setup, and how have you implemented it as a factory?

things to look out for

  1. Make sure your session factory is a singleton
  2. Create a session as PerWebRequest, using the OpenSession from the SessionFactory

    //Setup the Hibernate dependencies
        container.AddFacility<FactorySupportFacility>().Register(
            Component.For<ISessionFactory>().LifeStyle.Singleton
                .Instance(NHibernateHelper.GetSessionFactory()),
            Component.For<ISession>().LifeStyle.PerWebRequest
                .UsingFactoryMethod(kernel => kernel.Resolve<ISessionFactory>().OpenSession())
            );
    

    once you have registerd castle with MVC as a controller factory, it should only create a single session PerWebRequest and only if the controller, or its dependants are dependent on it

I have a sample app if it helps

2 Comments

Container = new WindsorContainer();
intriguing, im not sure what the MvcApplication.CurrentSession, I guess its static (I am not a fan of static in web apps). Coming back to the code, try changing to a PerWebRequest over Transient for the ISession
0

I use Spring.NET, not Castle Windsor, but I assume the concepts are the same. The scope of your ISession should be defined as per request, and shouldn't actually be created until asked for, as dbones says. While MVC handles every request, asking for a .jpg shouldn't hit a controller which depends on ISession.

Is Castle Windsor eagerly creating per request objects instead of on demand? That may be a configuration issue. Or do you have a custom module, handler or a Global.asax application method that is asking Windsor for an ISession?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.