我发现在不活动期间向我们的网络角色发出请求会导致请求非常慢(最多30秒)。在初始请求之后,角色将按预期执行。
经过大量谷歌搜索,我遇到了四种不同的策略(如下所列):
RoleEntryPoint.OnStart()
public override bool OnStart()
{
using (var server = new ServerManager())
{
server.ApplicationPoolDefaults.ProcessModel.IdleTimeout = TimeSpan.Zero;
server.CommitChanges();
}
return base.OnStart();
}
这也要求角色在较高的水平上运行。
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);
}
}
preloadEnabled
startMode
和RoleEntryPoint.OnStart()
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();
}
我应该执行以下哪些策略?
答案 0 :(得分:16)
我们使用了几个答案的组合,它对我们来说效果非常好,他们可以非常快速地进行更改和测试,但它似乎涵盖了所有基础。
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();
}
答案 1 :(得分:3)
您是否考虑过使用Azure端点监控来监控和触发您的角色每5分钟响应一次?它内置于Azure中,无需代码。
http://azure.microsoft.com/en-us/documentation/articles/web-sites-monitor/