asp.net-mvc - Azure 是否会阻止同时回收角色实例?

标签 asp.net-mvc iis azure azure-web-roles azure-cloud-services

我有一个 Web 角色部署到两个实例,应用程序池回收超时设置为默认值 29 小时,应用程序池空闲超时设置为零。我希望保持此应用程序池回收超时,以确保我的应用程序随着时间的推移保持健康。但是,我不希望我的两个实例(意外地)同时回收,以确保我的应用程序保持对用户的响应。

azure是否会注意多个实例的应用程序池不会同时回收?或者:我怎样才能防止这种情况发生?

最佳答案

Azure 不监视 w3wp 或您的应用程序池,也不协调不同实例之间的回收时间。为了防止应用程序池在多个实例之间同时回收,您应该修改每个实例的时间,例如 <29 小时 + IN_# * 1 小时> 这样 IN_0 将设置为 29 小时,IN_1 设置为 30, IN_2 31 岁等。

我的一位同事提供了这段代码:

using System;
using System.Threading.Tasks;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.ServiceRuntime;
using Microsoft.Web.Administration;

namespace RoleEntry
{
    public class Role : RoleEntryPoint
    {
        public override bool OnStart()
        {
            // For information on handling configuration changes
            // see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357.
            int instanceScheduleTime = 0;

            int.TryParse(RoleEnvironment.CurrentRoleInstance.Id.Substring(RoleEnvironment.CurrentRoleInstance.Id.LastIndexOf("_") + 1),out instanceScheduleTime);

            string roleId = string.Format("{0:D2}",(instanceScheduleTime % 24));
            TimeSpan scheduledTime = TimeSpan.Parse(roleId + ":00:00");

            using (ServerManager serverManager = new ServerManager())
            {
                 Configuration config = serverManager.GetApplicationHostConfiguration();

                ConfigurationSection applicationPoolsSection = config.GetSection("system.applicationHost/applicationPools");
                ConfigurationElement applicationPoolDefaultsElement = applicationPoolsSection.GetChildElement("applicationPoolDefaults");
                ConfigurationElement recyclingElement = applicationPoolDefaultsElement.GetChildElement("recycling");
                ConfigurationElement periodicRestartElement = recyclingElement.GetChildElement("periodicRestart");
                ConfigurationElementCollection scheduleCollection = periodicRestartElement.GetCollection("schedule");     

                bool alreadyScheduled = false;
                foreach (ConfigurationElement innerSchedule in scheduleCollection)
                {
                    if ((TimeSpan)innerSchedule["value"] == scheduledTime)
                        alreadyScheduled = true;
                }

                if (!alreadyScheduled)
                {
                    ConfigurationElement addElement1 = scheduleCollection.CreateElement("add");
                    addElement1["value"] = scheduledTime;
                    scheduleCollection.Add(addElement1);
                    serverManager.CommitChanges();
                }
            }

           return base.OnStart();
        }
    }
}

关于asp.net-mvc - Azure 是否会阻止同时回收角色实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28674888/

相关文章:

c# - 将 JSON 反序列化为在局部 View 中呈现的模型(restsharp 或任何其他方法)

asp.net - 自安装 VS 2012 以来,我遇到了间歇性 PlatformNotSupportedException

angular - IIS 允许路由到虚拟目录

asp.net-mvc - 在 MVC2 站点中托管 WCF

asp.net-mvc - 有没有完美的代码生成工具生成MVC Storefront?

jquery - Ajax 提交在首次提交后生成完整帖子

c# - .NET Core 2.0 MVC odd 404 on controller methods only when deployed to IIS

internet-explorer - 使用 https URL 和 http :443 URLs? 时出现 IE 混合内容警告

azure - 认知服务(LUIS、QnA 制造商)无法从 vNet 访问

azure - 使用用户名和密码获取 Azure Active Directory token