ASP.NET MVC 3 网站加载速度极慢
我真的不知道从哪里开始回答这个问题,但我正在工作的网站有时页面加载速度非常慢。特别是在构建之后,但并非总是如此。我通常需要刷新页面 5-10 次才能真正出现。我想我正在尝试看看我到底应该从哪里开始寻找。
ASP.NET MVC 3 忍者 自动映射器 实体框架代码优先 4.1 SQL Server 2008 Razor
更新
关于某些问题,它可以在每个页面上执行这么长时间的加载,但在所有页面上加载后速度相当快。
发布此内容并收到您的回复后,我启动了该应用程序,它仍在加载,并且可能永远不会加载,除非我在浏览器上单击“重新加载”。
没有缓存,而且 EF 模型并不大。
我使用的是 Razor 和具有 6 GB 内存和 I7 处理器的 Visual Studio 2010。
我在调试时使用 IIS Express 和默认 Web 服务器。它也在主服务器上的 IIS7 上执行此操作。
我可能会查看 MVC Profiler 和 Glimpse 来看看我能找到什么。
下面我有一些在访问主页时运行的代码。我想说当我第一次启动服务器时它永远不会加载。我在 var 模型上设置了一个断点,它永远不会被击中。如果我重新加载页面就会这样。
public ActionResult Index()
{
var model = new HomeViewModel();
model.RecentHeadlines = _headlineService.GetHeadlines(1, Config.RecentHeadlinesPageSize, string.Empty);
return View(model);
}
下面也是我的数据上下文设置。
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();
}
}
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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试 Glimpse 或使用 ASP.NET 跟踪。
您还可以预编译您的视图 如果您通过 适用于 MVC 的 Razor 单文件生成器。
Try Glimpse or use ASP.NET Tracing.
You could also precompile your views if you are using the Razor view engine via Razor Single File Generator for MVC.
这取决于您之前运行中发生的情况,有时如果您抛出错误并且没有清除该错误,那么您在运行应用程序时会遇到问题。如果出现错误,它有助于每次构建时重新启动浏览器。
但是,这可能是缓存问题。由于上下文处理维护不善,您的数据库可能正在缓存。这将导致查找在页面中遇到时运行得越来越快。确保在完成数据库事务后始终调用 .dispose()。
It depends on what happened in your previous run, sometimes if you throw an error and don't clear that out then you will have issues running the application. It helps to restart the browser every time you build if there was an error.
However, this could be an issue of caching. It is possible that your database is caching due to poorly maintained context disposing. This would cause the lookups to run faster and faster as they were encountered in pages. Make sure you always call .dispose() when done with your database transactions.
有趣 - 我曾经注意到 Unity 和 MVC 中出现过类似的情况,但我相信问题已自行解决。您还可以尝试 ants profiler 来查看问题是否出在 MVC 之外。
如果你让一个请求坐在那里(没有请求 5 次以上)会发生什么?
让单个请求运行 - 您的任何代码是否命中? (设置日志记录 log4net、nlog 等)以运行 application_start 等以查看编译后是否调用任何代码。
funny - I've noticed something similar once with unity and mvc but the problem I believe resolved itself. You could also try ants profiler to see if the problem is outside of MVC.
If you let a single request sit there (without requesting 5+ times) what happens?
Let a single request run - is ANY of your code hit? (setup logging log4net, nlog, etc) to run application_start, etc to see if any code is getting called after the compile.
我首先检查 IIS 中为进程自行回收而设置的超时时间。
我也是 MVC Mini-Profiler 的忠实粉丝,它可以准确地显示页面加载的各个部分花费了多长时间,一定要看看它。
编辑:
值得注意的是 Glimpse 项目 如今也非常适合这项任务。
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.
听起来,如果您在构建后或闲置一段时间后遇到 IIS AppPool 回收问题,这可能是一个问题。
为了帮助解决 AppPool 超时问题,您可以使用批处理文件我的创建是为了帮助缓解这个问题。
这不会在新构建后为您解决问题,因为您的 ASP.NET MVC 应用程序需要在首次运行时进行 JIT 编译。如果您确实渴望消除该问题,可以使用 ASP.NET 预编译< /a>.
Sounds like it might be an issue with IIS AppPool recycling if you're experiencing it after builds or after periods of inactivity.
To help with AppPool timeouts you can utilize a batch file I created to help mitigate the issue.
That won't solve the problem for you after new builds because your ASP.NET MVC application needs to be JIT-compiled upon first run. If you're really eager to eliminate that issue, you can use ASP.NET precompliation.