私のウェブサイトはMVC3、Entity Frameworkで構築されており、AutoFacはIoCコンテナとして使用されています。今大きな問題は、Webサイトのw3wp.exeプロセスがあまりにも多くのメモリを取っていることです。 Webサイトが起動すると、少し時間がかかって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;
}
}
ここに表示できるコードやコードに問題をトラッキングする必要があります。いくつかのタイプのオートファック登録は、私が推測するほどのメモリを食べることはありません。 – Jan
私はいくつかのコードを追加します – westonfw
あなたの 'IUnitOfWork'は' IDisposable'を実装していますか?あなたのDbContext/ObjectContextは、リクエストの後に破棄されないかもしれません。 – Eranga