I have a problem with my MVC3 Application on the prod server (I don’t have any problem in VS12).
- jQuery 1.8.2
- AutoFaq
- Repository Pattern
- log4net
- AutoMapper
The application has in the _Layout.cshtml some lists which has to be updated every 5 seconds via jQuery.
The code looks more or less like this:
<script type="text/javascript">
var products = $("#products ul");
function updateProducts() {
setTimeout(function() {
$.post("/Product/List", { rows: 10 }, function(html) {
products.before(html).remove();
products = $("#products ul");
// To avoid multiple posts update the list until the post is done
updateProducts();
});
}, 5000);
</script>
And the Controller looks like this:
public PartialViewResult List(int rows)
{
var products = productService.GetNewest(rows);
return PartialView(products);
}
The Service looks like this:
public class ProductService : Service<Product>, IProductService
{
private IRepository<Product> repository;
public ProductService(IUnitOfWorkFactory unitOfWorkFactory)
{
IUnitOfWork unitOfWork = unitOfWorkFactory.Create();
this.repository = unitOfWork.ProductRepository();
base.Repository = this.repository;
base.UnitOfWork = unitOfWork;
}
}
public List<Product> GetNewest(int rows)
{
var products = repository.GetAllQueryable(); //=> returns a IQueryable
products.OrderBy(o => o.CreateDate).Take(rows);
return products.ToList();
}
Repository (EF 5.0 code first):
public class Repository<T> : IRepository<T> where T : Base
{
private RepositoryContext _dataContext;
private IDbSet<T> _dbset;
public virtual IQueryable<T> GetAllQueryable()
{
var all = _dbset;
return all;
}
}
UnitOfWork:
public class UnitOfWork : Disposable, IUnitOfWork
{
private RepositoryContext _dataContext;
private Repository<Product> _productRepository;
public IUnitOfWork GetCurrent()
{
_dataContext = _dataContext ?? (_dataContext = new RepositoryContext());
return this;
}
public IRepository<Product> ProductRepository()
{
_productRepository = _productRepository ?? new Repository<Product>(_dataContext);
return _productRepository;
}
}
And the partial view contains only a foreach which returns the product name.
Even if I have only one list which has to be updated, the page loads getting slower and it’s randomly. I also use log4net to check if something happens when jQuery tries to get the products, but the Logs are empty :).
I also investigate the problem with MiniProfiler. The slowest thing (up to 15s) is the ASP.NET Begin Request.
Prod Server:
- Windows 2008
- 3GB Ram
- 2 other .NET Applications
- SQL08
EDIT
It seems the problem is fixed, YAY! Thanks to @vtortola for the hint with the Session. I did some research and found good links. Then I created a new Controller which has [SessionState(SessionStateBehavior.ReadOnly)] on it. And that’s it, an attribute solved my problem :)!
Here is a helpfull link, especially when you want a SessionStateBehavior like ReadAndWrite
http://www.davidcoombes.co/?p=151
How many lists do you update at the time? The session object uses a pesimisctic concurrency approach, what means that only one request can be processed at the time for the same session. If you are doing multiple requests in parallel, try to disable session in that controller, or make the session read only. Take a look at the SessionStateAttribute.
Cheers.