【Azure 云服务】在Cloud Service的代码中如何修改IIS Application Pool的配置呢? 比如IdleTimeout, startMode, Recycling.PeriodicRestart.Time等
什么是 PaaS?Platform as a Service
平台即服务 (PaaS) 是云中的完整开发和部署环境,你可以使用其中资源交付内容,从基于云的简单应用到启用云的复杂企业应用程序皆可。你以即用即付的方式从云服务提供商处购买所需资源,并通过安全的 Internet 连接访问这些资源。
类似 IaaS,PaaS 也包括服务器、存储空间和网络等基础结构,但它还包括中间件、开发工具、商业智能 (BI) 服务和数据库管理系统等。PaaS 旨在支持 Web 应用程序的完整生命周期:生成、测试、部署、管理和更新。
PaaS 让你无需购买和管理软件许可证、底层应用程序基础结构和中间件、容器业务流程协调程序(如 Kubernetes)或开发工具及其他资源,从而避免了开支和复杂操作。你管理自己开发的应用程序和服务,剩余事项一般由云服务提供商负责。
问题描述
Azure Cloud Service: 是平台即服务 (PaaS) 的一个示例,是一项基于 HTTP 的服务,用于托管 Web 应用程序、REST API 和移动后端 。它托管在虚拟机 (VM) 上,通过 IIS 自动部署和托管应用。
所以,当我们想要修改IIS的一些默认的配置时,如何操作呢?
- IdleTimeout: 默认值为20分钟,如果在20分钟Site不活动的情况下(没有新请求进入),IIS将终止工作进程以释放资源。
 - Start Mode:工作进程启动模式, AlwaysRunning(一直运行:如果Application Pool正在运行,请立即启动w3wp.exe进程。) OnDemand(按需启动:如果Application Pool正在运行,则在有第一个入站应用程序请求时启动w3wp.exe进程)
 - Recycling.PeriodicRestart.Time(Regular Time Interval):默认值为1740分钟,29小时。当Application Pool每运行29小时后,会自动回收。然后重启Application Pool。
 
在部署云服务(Cloud Service)时,有多种方式实现以上的修改。如添加启动任务通过CMD命令修改IIS配置(见文末附件部分),或通过代码修改,在WebRole的OnStart()方法中实现(本文就介绍代码如何实现)。
实现代码
准备条件:参考官方快速入门,使用VS 2019快速创建Cloud Service项目(Azure 云服务(经典)和 ASP.NET 入门)

第一步:在WebRole.cs文件 OnStart()方法中加入对Servicer Manager的修改代码。在使用时候,需要引用Microsoft.Web.Administration.dll。文件路径为:C:\Windows\System32\inetsrv\Microsoft.Web.Administration.dll

第二步:修改IIS Application Pool
- preloadEnabled:设置为ture,当Application Pool启动时,会自动想站点发送一个假请求。使得W3WP.EXE进程启动,当真实的第一个请求进入时马上进入业务处理,而避免为启动站点而消耗的时间。它与startMode="AlwaysRunning"配合
 - startMode:设置为AlwaysRunning
 - IdleTimeout:设置为0,表示没有闲置时间,W3WP.EXE进程不会自动终止
 - PeriodicRestart Time:设置为0,表示Applicaiton Pool不进行回收。
 - periodicRestart schedule:设置为05:00:00, 表示每天早上5点计划重启Application Pool。
 
public override bool OnStart()
{
ServicePointManager.DefaultConnectionLimit = 12;
if (!RoleEnvironment.IsEmulated)
{
using (ServerManager serverManager = new ServerManager())
{
// 1.参照https://stackoverflow.com/questions/24676194/azure-web-role-warm-up-strategies
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; //// 2.参照https://docs.microsoft.com/en-us/iis/configuration/system.applicationhost/applicationpools/add/recycling/periodicrestart/
//// < recycling logEventOnRecycle = "Schedule" >
//// < periodicRestart >
//// < schedule >
//// < add value = "03:00:00" />
//// </ schedule >
//// </ periodicRestart >
//// </ recycling >
ConfigurationElement periodicRestartElement = appPool.Recycling.GetChildElement("periodicRestart");
ConfigurationElementCollection scheduleCollection = periodicRestartElement.GetCollection("schedule");
ConfigurationElement addElement1 = scheduleCollection.CreateElement("add");
addElement1["value"] = TimeSpan.Parse("05:00:00");
scheduleCollection.Add(addElement1);
}
serverManager.CommitChanges();
}
}
// For information on handling configuration changes
// see the MSDN topic at https://go.microsoft.com/fwlink/?LinkId=166357.
return base.OnStart();
}
第三步:提交修改 ( serverManager.CommitChanges();)
第四步:在部署的时候,由于修改IIS配置需要提升权限,所以在发布时候需要在WebRole的配置文件(ServiceDefinition.csdef)中设置 <Runtime executionContext="elevated" />

发布后RDP到示例中验证

附录一:使用启动任务方式修改IIS配置
Follow the simple steps to gain granular control over Web Role process.
Create
Startup.cmdto the project root folder referenced by your web role project.Change content properties to
Copy if newerforStartup.cmdAdd following code to
Startup.cmd- as needed.
Disable idle%windir%\system32\inetsrv\appcmd set config -section:applicationPools -applicationPoolDefaults.processModel.idleTimeout:00:00:00
Auto start
%windir%\system32\inetsrv\appcmd set config -section:applicationPools -applicationPoolDefaults.autoStart:true
Always running
%windir%\system32\inetsrv\appcmd set config -section:applicationPools -applicationPoolDefaults.startMode:AlwaysRunning
Disable recycling
%windir%\system32\inetsrv\appcmd set config -section:applicationPools -applicationPoolDefaults.recycling.periodicRestart.time:00:00:00
Save the file as UTF-8 without a signature. (Tip: File Menu > Advanced Save Options in Visual Studio.)
Test the same in
Azure Emulator Express- making sure that the code is not breaking anything.Modify
ServiceDefinition.csdefto add the task andelevatedattribute.<ServiceDefinition name="NilayCornerService" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition">
<WorkerRole name="NilayCornerWorkerRole1">
...
<Startup>
<Task commandLine="Startup.cmd" executionContext="elevated" taskType="simple" />
</Startup>
</WorkerRole>
</ServiceDefinition>
Tip for advanced usage
To see a list of available options per section, try the following command
%windir%\system32\inetsrv\appcmd set config -section:applicationPools -?
附录二:部署时遇见的错误
| 
 Unhandled Exception: Filename: Error: Unrecognized element 'recycling' at Microsoft.Web.Administration.Interop.IAppHostElement.GetElementByName(String bstrSubName) at Microsoft.Web.Administration.ConfigurationElement.GetChildElement(String elementName) at WebRole1.WebRole.OnStart() in C:\Users\bulu\source\repos\AzureCloudService3\WebRole1\WebRole.cs:line 43 at Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.InitializeRoleInternal(RoleType roleTypeEnum) at Microsoft.WindowsAzure.ServiceRuntime.Implementation.Loader.RoleRuntimeBridge.<InitializeRole>b__0() at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() ' [2021-03-25T08:47:15Z] Last exit time: [2021/03/25, 08:47:15.149]. Last exit code: 0.  | 
| 
 代码中出现重复获取Recycling节点 ConfigurationElement periodicRestartElement = appPool.Recycling.GetChildElement("recycling").GetChildElement("periodicRestart"); 修改为 ConfigurationElement periodicRestartElement = appPool.Recycling.GetChildElement("periodicRestart");  | 
| 修改后,问题消失。 | 
参考资料
IIS: Idle Timeout vs Recycle:https://stackoverflow.com/questions/19985710/iis-idle-timeout-vs-recycle
Azure Web Role “warm up” strategies [closed]:https://stackoverflow.com/questions/24676194/azure-web-role-warm-up-strategies
Periodic Restart Settings for Application Pool Recycling <periodicRestart>:https://docs.microsoft.com/en-us/iis/configuration/system.applicationhost/applicationpools/add/recycling/periodicrestart/
Azure 云服务(经典)和 ASP.NET 入门:https://docs.azure.cn/zh-cn/cloud-services/cloud-services-dotnet-get-started
【Azure 云服务】在Cloud Service的代码中如何修改IIS Application Pool的配置呢? 比如IdleTimeout, startMode, Recycling.PeriodicRestart.Time等的更多相关文章
- 【Azure 云服务】Azure Cloud Service 创建 Alert 指南 [基于旧版 Alert(Classic)不可用情况下]
		
问题描述 在Azure云服务(Cloud Service)创建Alert(Classic)时候遇见失败消息:"Failed to update alert testclassicalertr ...
 - Azure 云服务中的实例端点
		
Azure云服务(cloud Servive)中有三种端点类型(endpoint type):输入端点(input);内部端点(internal);实例端点(InstanceInput) 1.输入端点 ...
 - 【Azure云服务 Cloud Service】如何在部署云服务Cloud Service时候通过启动任务Start Task来配置IIS (如开启ARR)
		
问题情形 通过VS部署Cloud Service时,需要在开始任务时候安装或配置其他任务,如安装及配置ARR. 执行步骤 1) 下载 requestRouter_amd64.msi 和 webfarm ...
 - 【Azure 云服务】Azure Cloud Service 为 Web Role(IIS Host)增加自定义字段 (把HTTP Request Header中的User-Agent字段增加到IIS输出日志中)
		
问题描述 把Web Role服务发布到Azure Cloud Service后,需要在IIS的输出日志中,把每一个请求的HTTP Request Header中的User-Agent内容也输出到日志中 ...
 - 【Azure 云服务】Azure Cloud Service在发布新部署后遇见不能RDP(远程连接)到实例时如何处理?
		
Azure 云服务是PaaS 的一个示例. 与 Azure 应用服务一样,此技术设计用于支持可缩放.可靠且运营成本低廉的应用程序. 同样,应用服务托管在虚拟机 (VM) 上,Azure 云服务也是如此 ...
 - 利用VisualVM监测Azure云服务中的Java应用
		
在做Java开发的时候,我们需要在上线之前对程序进行压力测试,对程序的性能进行全面的监控,了解JVM的CPU,内存,GC,classes,线程等等信息,或者在程序上线运行的过程当中以便于诊断问题或者对 ...
 - [转]在 Azure 云服务上设计大规模服务的最佳实践
		
本文转自:http://technet.microsoft.com/zh-cn/magazine/jj717232.aspx 英文版:http://msdn.microsoft.com/library ...
 - 调试 Azure 云服务项目的方法
		
调试 Azure 云服务项目 在菜单栏中选择“调试”>“启动调试”(键盘操作:F5). 重要提示 如果未将 Azure 云服务项目设置为启动项目,当你单击按钮以触发断点时,将出现以下错误:“外部 ...
 - 面向阿里云专家的 Azure 云服务介绍
		
本文是面向阿里云专家的 Azure 云服务介绍,参考本文可以帮助大家“按图索骥”在 Azure 的平台上找到能满足自己需求的服务. 在公有云计算蓬勃发展的同时,中国也出现了越来越多的本土公有云平台.针 ...
 
随机推荐
- 通过改写 原始对象的paint 方法,来设置对象的border颜色
			
解决方案: //通过改写 原始对象的paint 方法,来设置对象的border颜色 1. package test1;import java.awt.Color;import java.awt.Gra ...
 - Apple CSS Animation All In One
			
Apple CSS Animation All In One Apple Watch CSS Animation https://codepen.io/xgqfrms/pen/LYZaNMb See ...
 - cookie & maxAge & expires
			
cookie & maxAge & expires Expires (timestamp) & Max-Age (seconds) https://developer.mozi ...
 - umi
			
umi https://github.com/umijs/umi https://umijs.org/zh/guide/ dva https://github.com/dvajs/dva https: ...
 - bash shell cli tools
			
bash shell cli tools before # create files $ mkdir app-component $ cd app-component $ touch index.ht ...
 - js字典
			
传送门https://www.2cto.com/kf/201709/680989.html 点击
 - 2021-02:Teams开发平台更新概述
			
作为2021年工作计划的一部分,我会在每月的第三个星期五发布Teams开发平台的更新报告,给大家整理和讲解最新的平台功能,以及特色场景.这是第一篇文章,我会挑选截至到2月份一些重要的更新,以后每月的更 ...
 - Windows定时重新启动(适用于server 2012 r2)
			
直接看链接吧:https://jingyan.baidu.com/article/2d5afd69dd8e9d85a2e28eb7.html 开始菜单,找到"计划任务程序"; 2 ...
 - 微信小程序:将yyyy-mm-dd格式的日期转换成yyyy-mm-dd hh:mm:ss格式的日期
			
代码如下: changeDate1(e) { console.log(e); var date = new Date(e.detail.value); console.log(date); const ...
 - SpringBoot解决特殊符号 []报400问题
			
当遇到特殊符号传递给后台时,如果不加处理,就会报400的错误,解决办法有两种. 1.前台解决 前台解决的方法就是把这些特殊符号转义,转义之后浏览器和后台都可以识别. //对特殊字符进行转义 encod ...
 
			
		