我有一个 ASP.NET MVC 网站,我已经验证它在每次第一次点击新的 C# 文件(如 Controller )时都在编译。我查看了任务管理器,每次第一次点击新 Controller 时,页面都很慢,由于编译器,CPU 达到峰值。
我之前使用过 Rosyln 编译器,但我没有做任何更改就切换回了常规的 C# 编译器。
我尝试过预编译,但是当我将我的网站复制到网络托管计算机时似乎并不重要。
我不记得在我使用过的以前版本的应用程序中发生过这种情况,但其中大部分主要是 ASP.NET 表单与 MVC 的混合。
这是正常行为还是我可以通过设置来纠正?我希望它在首次部署站点时编译所有文件。 (实际上,第一页太长了,我不确定它是如何不这样做的)
现在,我有一个脚本,可以在我部署应用程序后访问每个 Controller ,从而避免出现问题。
要复制,只需将一个新的主 dll 复制到您的 bin 文件夹中。然后在您浏览具有不同 Controller 的不同页面时查看您的任务管理器。
最佳答案
startMode
您的应用程序池始终运行,这将阻止您的应用程序池在一段时间后休眠。有关更多信息,请阅读以下帖子:
示例:
public class RecycleConfig
{
public static void PingSite()
{
using (var refresh = new WebClient())
{
try
{
refresh.DownloadString("http://yoursote.com");
}
catch (Exception)
{
}
}
}
public static void SetupRefreshJob()
{
if (HttpContext.Current == null) return;
//remove a previous job
Action remove = HttpContext.Current.Cache["Refresh"] as Action;
if (remove is Action)
{
HttpContext.Current.Cache.Remove("Refresh");
remove.EndInvoke(null);
}
//get the worker
Action work = () =>
{
while (true)
{
Thread.Sleep(600000);
PingSite();
}
};
work.BeginInvoke(null, null);
//add this job to the cache
HttpContext.Current.Cache.Add("Refresh",
work,
null,
Cache.NoAbsoluteExpiration,
Cache.NoSlidingExpiration,
CacheItemPriority.Normal,
(s, o, r) => { SetupRefreshJob(); }
);
}
}
在
Global.asax
添加 - protected void Application_Start()
{
RecycleConfig.SetupRefreshJob();
/........
/........
}
protected void Application_End(object sender, EventArgs e)
{
RecycleConfig.PingSite();
}
关于c# - 每个文件在 ASP.NET MVC 站点上的初始加载都很慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61830144/