Azure应用或云服务,在加载平衡器之前运行一些代码 [英] Azure app or cloud service, run some code before load balncer
问题描述
对于Azure中的Cloud Service(可能也是App Service),如何在交换服务可用之前或新实例中运行一些代码.
In case of Cloud Service in Azure (maybe also App Service), how can I run some code before the service become available in swap or in case of new instance.
例如,在第一个用户访问之前将数据加载到缓存.
for example, loading data to cache before the first user have access.
推荐答案
具有角色的云服务
将您的应用初始化代码放入OnStart()
中.
public class WorkerRole : RoleEntryPoint
{
public override bool OnStart()
{
try
{
// Add initialization code here
}
catch (Exception e)
{
Trace.WriteLine("Exception during OnStart: " + e.ToString());
// Take other action as needed.
}
return base.OnStart();
}
}
在
OnStart
方法返回之前,角色实例的状态设置为忙",并且该实例无法通过负载均衡器获得.
Before the
OnStart
method returns, the status of the role instance is set to Busy and the instance is not available through the load balancer.
如果OnStart
方法返回false,则角色实例立即停止.如果该方法返回true,则Windows Azure通过调用Run方法来启动角色.通常,应该避免从OnStart
方法返回false.
If the OnStart
method returns false, the role instance is immediately stopped. If the method returns true, Windows Azure starts the role by calling the Run method. In general, you should avoid returning false from the OnStart
method.
应用服务
使用应用程序初始化 IIS模块.此处详细描述了该机制- http://ruslany.net/2015/09/how-to-warm-up-azure-web-app-during-deployment-slots-swap/
App Service
Use the Application Initialization IIS module. The mechanism is described in detail here - http://ruslany.net/2015/09/how-to-warm-up-azure-web-app-during-deployment-slots-swap/
web.config :
<system.webServer>
<applicationInitialization >
<add initializationPage="/warmup-cache.php" hostName="site.azurewebsites.net"/>
</applicationInitialization>
<system.webServer>
这篇关于Azure应用或云服务,在加载平衡器之前运行一些代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!