我发现在不活动的时间段之后请求我们的网络角色会导致非常慢的请求(最多30秒)。在该初始请求之后,该角色将按原样执行。
经过很多谷歌搜索,我遇到了四种不同的策略(如下所示):
(a)在RoleEntryPoint.OnStart()中禁用IIS空闲超时
public override bool OnStart() { using (var server = new ServerManager()) { server.ApplicationPoolDefaults.ProcessModel.IdleTimeout = TimeSpan.Zero; server.CommitChanges(); } return base.OnStart(); }
这也要求角色在较高的水平上运行。
(b)在RoleEntryPoint.Run()中定期执行请求
public override void Run() { var localuri = new Uri(string.Format("https://{0}/Help",RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["HttpsIn"].IPEndpoint)); while (true) { try { var request = (HttpWebRequest)WebRequest.Create(localuri); request.Method = "GET"; var response = request.GetResponse(); } catch { } System.Threading.Thread.Sleep(3000); } }
(c)在RoleEntryPoint.OnStart()中设置preloadEnabled和startMode
public override void OnStart() { using (var serverManager = new ServerManager()) { foreach (var application in serverManager.Sites.SelectMany(x => x.Applications)) { application["preloadEnabled"] = true; } foreach (var applicationPool in serverManager.ApplicationPools) { applicationPool["startMode"] = "AlwaysRunning"; } serverManager.CommitChanges(); } return base.OnStart(); }
(d)最后,使用Azure的“Always On”(编辑:这只是Azure网站不幸!)
我应该执行哪些策略?
解决方法
我们结合了几个这样的答案,它对我们来说是非常好的,但是它们非常快速的改变和测试,似乎覆盖了所有的基础。
public override bool OnStart() { ServicePointManager.DefaultConnectionLimit = 12; if(!RoleEnvironment.IsEmulated) { using(ServerManager serverManager = new ServerManager()) { foreach (var app in serverManager.Sites.SelectMany(x => x.Applications)) { app["preloadEnabled"] = true; } foreach (var appPool in serverManager.ApplicationPools) { appPool.AutoStart = true; appPool["startMode"] = "AlwaysRunning"; appPool.ProcessModel.IdleTimeout = TimeSpan.Zero; appPool.Recycling.PeriodicRestart.Time = TimeSpan.Zero; } serverManager.CommitChanges(); } } return base.OnStart(); }