ASP.NET MVC 3 Site Loading Is Extremely Slow

Mike Flynn picture Mike Flynn · Dec 15, 2011 · Viewed 20.1k times · Source

I really don't know where to begin with this question, but the site I'm working on at times has some really slow page loads. Especially after doing a build, but not always. I usually have to refresh the page 5-10 times before it actually comes up. I guess I am trying to see where exactly I should begin to look.

ASP.NET MVC 3 Ninject AutoMapper Entity Framework Code First 4.1 SQL Server 2008 Razor

UPDATES

Concerning some of the questions, it can do this long loading on every page, but after it loads its fairly quick on all the pages.

After posting this and getting your replies I started the application and it is still loading and probably won't ever load unless I click reload on the browser.

No caching, and the EF models aren't huge.

I am using Razor and Visual Studio 2010 with 6 GB of memory and an I7 processor.

I am using IIS Express and the default web server when debugging. It also does this on IIS7 on the main server.

I may look into the MVC Profiler and Glimpse to see what I can find.

Below I have some code this runs when it hits the homepage. I would say it never loads when I first start up the server. I put a break point at var model which never gets hit. If I reload the page then it does.

public ActionResult Index()
        {
            var model = new HomeViewModel();

            model.RecentHeadlines = _headlineService.GetHeadlines(1, Config.RecentHeadlinesPageSize, string.Empty);

            return View(model);
        }

Below is my datacontext setup also.

public class DatabaseFactory : Disposable, IDatabaseFactory
    {
        private DataContext _dataContext;
        public DataContext Get()
        {
            return _dataContext ?? (_dataContext = new DataContext());
        }
        protected override void DisposeCore()
        {
            if (_dataContext != null)
                _dataContext.Dispose();
        }
    }

public class Disposable : IDisposable
    {
        private bool isDisposed;

        ~Disposable()
        {
            Dispose(false);
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        private void Dispose(bool disposing)
        {
            if (!isDisposed && disposing)
            {
                DisposeCore();
            }

            isDisposed = true;
        }

        protected virtual void DisposeCore()
        {
        }
    }

public class UnitOfWork : IUnitOfWork
    {
        private readonly IDatabaseFactory _databaseFactory;
        private DataContext _dataContext;

        public UnitOfWork(IDatabaseFactory databaseFactory)
        {
            _databaseFactory = databaseFactory;
        }

        protected DataContext DataContext
        {
            get { return _dataContext ?? (_dataContext = _databaseFactory.Get()); }
        }

        public void Commit()
        {
            DataContext.Commit();
        }
    }

Answer

sclarson picture sclarson · Dec 15, 2011

I'd start by checking what the timeouts are set to in IIS for the process to be recycling itself.

I'm also a very big fan of the MVC Mini-Profiler which could show you exactly how long various parts of your page load are taking, definitely take a look at it.

Edit:

It is worth noting that the Glimpse project is also great for this task these days.