我的网站是使用MVC3,Entity Framework构建的,而AutoFac则用作IoC容器。现在一个大问题是网站的w3wp.exe进程占用了太多内存。当网站启动时,它会在一段时间内使用300M,令牌内存最高可达1GB。我不知道如何解决它。
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;
}
}
答案 0 :(得分:1)
您使用的是缓存吗?
您可以使用内存分析器(例如 dotTrace )来查看占用内存的对象。
答案 1 :(得分:1)
如果您认为问题是未发布的组件,请查看whitebox:http://whitebox.googlecode.com
它可以自动找到与使用IoC相关的大多数泄漏。