我的网站是用MVC3,Entity Framework构建的,并且AutoFac被用作IoC容器。现在一个很大的问题是网站的w3wp.exe进程占用了太多内存。网站启动时,它会在一小段时间内使用300M,令牌内存高达1GB。我不知道如何解决它。mvc3 +实体框架+ autofac,网站占用太多内存
autofac配置
public static void Initialise () {
var builder = new ContainerBuilder();
builder.RegisterControllers(Assembly.GetExecutingAssembly());
builder.Register(c => new DatabaseFactory()).As<IDatabaseFactory>().InstancePerLifetimeScope();
builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().InstancePerLifetimeScope();
builder.RegisterType<ActionService>().As<IActionService>().InstancePerLifetimeScope();
builder.RegisterType<ArticleService>().As<IArticleService>().InstancePerLifetimeScope();
builder.RegisterType<BookmarkService>().As<IBookmarkService>().InstancePerLifetimeScope();
builder.RegisterType<BuildLogService>().As<IBuildLogService>().InstancePerLifetimeScope();
builder.RegisterType<NewsService>().As<INewsService>().InstancePerLifetimeScope();
builder.RegisterType<NewsTopicService>().As<INewsTopicService>().InstancePerLifetimeScope();
builder.RegisterType<QQAuthService>().As<IQQAuthService>().InstancePerLifetimeScope();
builder.RegisterType<QuestionAnswerService>().As<IQuestionAnswerService>().InstancePerLifetimeScope();
builder.RegisterType<QuestionService>().As<IQuestionService>().InstancePerLifetimeScope();
builder.RegisterType<QuestionSupplementService>().As<IQuestionSupplementService>().InstancePerLifetimeScope();
builder.RegisterType<RoleService>().As<IRoleService>().InstancePerLifetimeScope();
builder.RegisterType<UserService>().As<IUserService>().InstancePerLifetimeScope();
builder.RegisterType<ActionChannelRepository>().As<IActionChannelRepository>().InstancePerLifetimeScope();
builder.RegisterType<ActionRepository>().As<IActionRepository>().InstancePerLifetimeScope();
builder.RegisterType<ArticleCategoryRepository>().As<IArticleCategoryRepository>().InstancePerLifetimeScope();
builder.RegisterType<ArticleRepository>().As<IArticleRepository>().InstancePerLifetimeScope();
builder.RegisterType<BookmarkRepository>().As<IBookmarkRepository>().InstancePerLifetimeScope();
builder.RegisterType<BuildLogRepository>().As<IBuildLogRepository>().InstancePerLifetimeScope();
builder.RegisterType<NewsCategoryRepository>().As<INewsCategoryRepository>().InstancePerLifetimeScope();
builder.RegisterType<NewsRepository>().As<INewsRepository>().InstancePerLifetimeScope();
builder.RegisterType<NewsTopicRepository>().As<INewsTopicRepository>().InstancePerLifetimeScope();
builder.RegisterType<QQAuthRepository>().As<IQQAuthRepository>().InstancePerLifetimeScope();
builder.RegisterType<QuestionAnswerRepository>().As<IQuestionAnswerRepository>().InstancePerLifetimeScope();
builder.RegisterType<QuestionRepository>().As<IQuestionRepository>().InstancePerLifetimeScope();
builder.RegisterType<QuestionSupplementRepository>().As<IQuestionSupplementRepository>().InstancePerLifetimeScope();
builder.RegisterType<RoleRepository>().As<IRoleRepository>().InstancePerLifetimeScope();
builder.RegisterType<UserRepository>().As<IUserRepository>().InstancePerLifetimeScope();
IContainer container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
服务定义
public class ActionService : IActionService {
private readonly IActionRepository actionRepository;
private readonly IActionChannelRepository actionChannelRepository;
private readonly IUnitOfWork unitwork;
private readonly static ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public ActionService (IUnitOfWork unitwork, IActionRepository actionRepository, IActionChannelRepository actionChannelRepository) {
this.unitwork = unitwork;
this.actionRepository = actionRepository;
this.actionChannelRepository = actionChannelRepository;
}
`
public class NewsRepository : RepositoryBase<News>, INewsRepository {
public NewsRepository (IDatabaseFactory databaseFactory)
: base(databaseFactory) {
}
/// <summary>
/// 获取某人的新闻
/// </summary>
/// <param name="userid"></param>
/// <returns></returns>
public IEnumerable<News> GetNewsOfUser (int userid) {
return this.DB.News.Where(a => a.CreatorID == userid);
}
/// <summary>
/// 获取某人的新闻
/// </summary>
/// <param name="userid"></param>
/// <param name="pagesize"></param>
/// <param name="pageindex"></param>
/// <returns></returns>
public IEnumerable<News> GetNewsOfUser (int userid, int pagesize, int pageindex) {
if (pageindex < 1)
pageindex = 1;
int skipCount = (pageindex - 1) * pagesize;
return this.DB.News.Where(a => a.CreatorID == userid).OrderByDescending(a => a.CreateTime).Skip(skipCount).Take(pagesize);
}
/// <summary>
/// 分页
/// </summary>
/// <param name="pagesize"></param>
/// <param name="pageindex"></param>
/// <returns></returns>
public IEnumerable<News> GetNews (int pagesize, int pageindex) {
if (pageindex < 1)
pageindex = 1;
int skipCount = (pageindex - 1) * pagesize;
return this.DB.News.OrderByDescending(a => a.CreateTime).Skip(skipCount).Take(pagesize);
}
public int GetNewsCountOfTopic (int topicid) {
return this.DB.News.Where(a => a.TopicID == topicid).Count();
}
/// <summary>
/// 获取主题下的文章
/// </summary>
/// <param name="pagesize"></param>
/// <param name="pageindex"></param>
/// <param name="topicid"></param>
/// <returns></returns>
public IEnumerable<News> GetNewsByTopic (int topicid, int pagesize, int pageindex) {
if (pageindex < 1)
pageindex = 1;
int skipCount = (pageindex - 1) * pagesize;
return this.DB.News.Where(a => a.TopicID == topicid)
.OrderByDescending(a => a.CreateTime).Skip(skipCount).Take(pagesize);
}
public int GetNewsCountOfCategory (int categoryid) {
return this.DB.News.Where(a => a.NewsCategories.Select(c => c.ID).Contains(categoryid)).Count();
}
/// <summary>
/// 获取分类下的文章
/// </summary>
/// <param name="pagesize"></param>
/// <param name="pageindex"></param>
/// <param name="categoryid"></param>
/// <returns></returns>
public IEnumerable<News> GetNewsByCategory (int categoryid, int pagesize, int pageindex) {
if (pageindex < 1)
pageindex = 1;
int skipCount = (pageindex - 1) * pagesize;
return this.DB.News.Where(a => a.NewsCategories.Select(c => c.ID).Contains(categoryid))
.OrderByDescending(a => a.CreateTime).Skip(skipCount).Take(pagesize);
}
public void UpdateViewCount (int id) {
News temp = this.DB.News.FirstOrDefault(a => a.ID == id);
if (temp != null)
temp.PageViewCount += 1;
}
}
恐怕您必须将问题追踪到可以在此处显示的代码片段或代码区域。某些类型的autofac注册不会消耗我想象中的那么多内存。 – Jan 2011-12-20 13:57:33
我添加了一些代码 – westonfw 2011-12-20 14:07:20
你的'IUnitOfWork'是否实现'IDisposable'?您的DbContext/ObjectContext可能不会在请求后处理。 – Eranga 2011-12-20 14:21:00