Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8642999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:55:16+00:00 2026-06-12T11:55:16+00:00

I have a ASP.NET MVC4 web api that uses fluent NHibernate, but when I

  • 0

I have a ASP.NET MVC4 web api that uses fluent NHibernate, but when I run debug it takes 1½ min to access any site that uses the database the first time I try, so I found Fluent nHibernate Slow Startup Time but I am not sure how to utilize that,

When it runs on the live server it must garbage collect the session data as it takes ages to configure again if I havent used it for a long time.

Is it possiible to cut it down to maybe sub 10 seconds instead of 1.5 minutes?

My current SessionFactory class looks like this

public class SessionFactory
{
    private static readonly string ConnString = System.Configuration.ConfigurationManager.ConnectionStrings["MySQLConnectionString"].ConnectionString;

    private static ISessionFactory _session;
    private static readonly object SyncRoot = new Object();

    private static ISessionFactory CreateSessionFactory()
    {
        return Fluently.Configure()
            .Database(MySQLConfiguration
            .Standard
            .ConnectionString(ConnString))
            .Mappings(m => m.FluentMappings
                .AddFromAssemblyOf<UserMap>())
                .ExposeConfiguration(UpdateSchema)
                .BuildSessionFactory();
    }

    private static void UpdateSchema(Configuration cfg)
    {
        new SchemaUpdate(cfg).Execute(false, true);
    }

    public static ISession Session
    {
        get
        {
            if (_session == null)
            {
                lock (SyncRoot)
                {
                    if (_session == null)
                        _session = CreateSessionFactory();
                }
            }
            return _session.OpenSession();
        }
    }
}

Here is a screenshot of a dotTraceenter image description here

Timing the request on my host it takes 1 min and 50 seconds for the initial request, and thats on “live” server (I cant control the IIS there)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T11:55:17+00:00Added an answer on June 12, 2026 at 11:55 am

    I can see a host of things wrong with this code. Your CreateSessionFactory should only ever be called once in the lifetime of your application. Create a static variable in global.asax and call CreateSessionFactory once in Application_Start(). Also in global.asax, you want to setup some events for Application_BeginRequest and Application_EndRequest. This is where you are going to create and destory sessions. This is your unit of work in a web application. You should also store your session in the HttpContext.

    public static ISession CurrentSession
    {
        get { return (ISession)HttpContext.Current.Items[sessionkey]; }
        set { HttpContext.Current.Items[sessionkey] = value; }
    }
    
    protected void Application_BeginRequest()
    {
        CurrentSession = SessionFactory.OpenSession();
    }
    
    protected void Application_EndRequest()
    {
        if (CurrentSession != null)
            CurrentSession.Dispose();
    }
    

    Your code recreates the SessionFactory every time you need a session.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a service implemented in MVC4 / ASP.NET Web Api. I would like
I have an ASP.NET MVC 4 application which uses the new ASP.NET Web API.
I have an ASP.NET 4 web application (MVC 4 release candidate, NHibernate 3, Fluent-NHibernate)
I have a ASP.NET MVC 4 application with Web API. It's working great. But
I have a layered web application that I built with ASP.NET MVC 4, WebAPI
I have an ASP.NET MVC4 app which uses DotNetOpenAuth 4.0.1 to authenticate users with
Environment In my solution I have three projects, they are: Web (Asp.net MVC4) Model
I am building an API in ASP.NET MVC4 Web API, and one of my
I have a pretty standard asp.net web api set up with get post put
I have an ASP.NET MVC4 website implementing a REST API, which I'm consuming from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.