【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 的平台上找到能满足自己需求的服务. 在公有云计算蓬勃发展的同时,中国也出现了越来越多的本土公有云平台.针 ...
随机推荐
- 十三香 & 香料
十三香 & 香料 十三香原料组成不完全一致, 但有一些香料却是大家都会采用的: 草蔻.砂仁.肉豆蔻.肉桂.丁香. 花椒.大料.小茴香.木香.白芷. 山萘.良姜和姜 王守义十三香 http:// ...
- Web 全栈开发 MySQL 面试题
Web 全栈开发 MySQL 面试题 MySQL MySQL 读写分离 读写分离原理 MySQL的主从复制和MySQL的读写分离两者有着紧密联系,首先部署主从复制,只有主从复制完了,才能在此基础上进行 ...
- js & class & init
js & class & init how to call class init method in js when create an instance 在初始化类实例的时候调用,类 ...
- c++ 使用进程id获取打开的网络端口
#pragma warning( disable : 4996) #include <winsock2.h> #include <ws2tcpip.h> #include &l ...
- iPad pro & Mac mini
iPad pro & Mac mini
- asm movbe 指令
movbe MOVBE 目标操作数,源操作数 复制源操作数的数据,交换字节后,移动数据 假如: movbe eax,(float)1000.0 eax == 0x00007A44 movbe eax, ...
- 比特币大涨之际,VAST空投火爆来袭!
昨天特斯拉宣布投资了15亿美元的比特币,消息一出,币圈沸腾,比特币瞬间拉升,突破4万美元关口,现价46000美元!大涨20%,又刷新历史新高! 另外NGK项目热度最高的BGV也是不断刷新历史新高,数据 ...
- Unity3d 拖拽脚本报错Can't add the script component "" because the script class cannot be found
解决办法: ①报错原因:文件名与文件内容中的类名不相符. ②关闭360.鲁大师等防护软件,重新安装系统.
- RecycleView 点击事件监听
1.定义RecycleView 监听接口类 1 package com.example.*****; 2 3 import android.view.View; 4 5 public interfac ...
- Ctf_show Web[1-8]
CTFshow_web1: F12查看JS即在代码里 CTFshow_web2: 进入网址后页面如下: ①:尝试使用admin登陆,发现页面无回显 ②:尝试单引号闭合,并且添加' or 1=1#,此时 ...
