保持你的网站激活(不要让IIS回收您的网站)
你是否曾经打开你的一个网站在你最后显示网页的时候总要等待一段时间,你如果你遇到这个问题,你常常认为是IIS引起的,IIS在不断的回收利用网站资源。其实还有一种方法就是让网站不要“闲”下来,看以下代码:

private static void _SetupRefreshAction() {
//移除之前的动作
Action remove = HttpContext.Current.Cache["Refresh"] as Action;
if (remove is Action) {
HttpContext.Current.Cache.Remove("Refresh");
remove.EndInvoke(null);
}
//获取新的动作
Action work = () => {
while (true) {
Thread.Sleep(60000);
//这里代码开始刷新动作
}
};
work.BeginInvoke(null, null);
//增加动作到缓存中
HttpContext.Current.Cache.Add(
"Refresh",
work,
null,
Cache.NoAbsoluteExpiration,
Cache.NoSlidingExpiration,
CacheItemPriority.Normal,
(s, o, r) => { _SetupRefreshAction(); }
);
}
//移除之前的动作
Action remove = HttpContext.Current.Cache["Refresh"] as Action;
if (remove is Action) {
HttpContext.Current.Cache.Remove("Refresh");
remove.EndInvoke(null);
}
//获取新的动作
Action work = () => {
while (true) {
Thread.Sleep(60000);
//这里代码开始刷新动作
}
};
work.BeginInvoke(null, null);
//增加动作到缓存中
HttpContext.Current.Cache.Add(
"Refresh",
work,
null,
Cache.NoAbsoluteExpiration,
Cache.NoSlidingExpiration,
CacheItemPriority.Normal,
(s, o, r) => { _SetupRefreshAction(); }
);
}
把这段代码写在Global.asax的Application_Start(),这样我们开始一个动作时候让网站保持激活,当然你也可以使用Thread来保持这个刷新方法。
那么,该怎么保持网站的刷新呢?使用WebClient即可实现:
WebClient refresh = new WebClient();
try {
refresh.UploadString("http://www.mysite.com/", string.Empty);
}
catch (Exception ex) {
}
finally {
refresh.Dispose();
}
try {
refresh.UploadString("http://www.mysite.com/", string.Empty);
}
catch (Exception ex) {
}
finally {
refresh.Dispose();
}
这个代码片段使用WebClient发生红HTTP请求给我们的网站,是网站始终保持激活中。是不是很简单呢:)
分类:
.NET 综合
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App
· 张高兴的大模型开发实战:(一)使用 Selenium 进行网页爬虫
2009-01-03 Javascript乱弹设计模式系列(0) - 面向对象基础以及接口和继承类的实现