Ejemplo n.º 1
0
 internal static bool RemoveServers(string farmName, IEnumerable <string> endpoints)
 {
     using (ServerManager manager = new ServerManager()) {
         ConfigurationElementCollection webFarmsConfiguration = GetWebFarmsConfiguration(manager);
         ConfigurationElement           webFarm = GetWebFarmConfiguration(webFarmsConfiguration, farmName);
         if (webFarm != null)
         {
             ConfigurationElementCollection servers = webFarm.GetCollection();
             bool configurationChanged = false;
             foreach (string endPoint in endpoints)
             {
                 Dictionary <string, string> attributes = new Dictionary <string, string>()
                 {
                     { "address", endPoint }
                 };
                 ConfigurationElement serverConfiguration = ConfigurationElementUtils.FindElement(servers, "server", attributes);
                 if (serverConfiguration != null && ConfigurationElementUtils.HasAttribute(serverConfiguration, "address", endPoint))
                 {
                     servers.Remove(serverConfiguration);
                     configurationChanged = true;
                 }
             }
             if (configurationChanged)
             {
                 manager.CommitChanges();
             }
             return(true);
         }
     }
     return(false);
 }
Ejemplo n.º 2
0
        static ConfigurationElement GetWebFarmConfiguration(ConfigurationElementCollection webFarmConfiguration, string farmName)
        {
            var attributes = new ConfigurationElementAttributes()
            {
                { "name", farmName }
            };

            return(ConfigurationElementUtils.FindElement(webFarmConfiguration, WEB_FARM_TAGNAME, attributes));
        }
Ejemplo n.º 3
0
        internal static bool AddServers(string farmName, IEnumerable <string> endpoints, int port)
        {
            using (ServerManager manager = new ServerManager()) {
                ConfigurationElementCollection webFarmsConfiguration = GetWebFarmsConfiguration(manager);
                ConfigurationElement           webFarm = GetWebFarmConfiguration(webFarmsConfiguration, farmName);
                if (webFarm != null)
                {
                    ConfigurationElementCollection servers = webFarm.GetCollection();
                    bool configurationChanged = false;
                    foreach (string endPoint in endpoints)
                    {
                        Dictionary <string, string> attributes = new Dictionary <string, string>()
                        {
                            { "address", endPoint }
                        };
                        ConfigurationElement serverConfiguration = ConfigurationElementUtils.FindElement(servers, "server", attributes);
                        if (serverConfiguration == null)
                        {
                            serverConfiguration            = servers.CreateElement("server");
                            serverConfiguration["address"] = endPoint;
                            serverConfiguration["enabled"] = true;

                            ConfigurationElement connectionConfiguration = serverConfiguration.GetChildElement("applicationRequestRouting");
                            connectionConfiguration["httpPort"] = port;

                            servers.Add(serverConfiguration);
                            configurationChanged = true;
                        }
                    }
                    if (configurationChanged)
                    {
                        manager.CommitChanges();
                    }
                    return(true);
                }
            }
            return(false);
        }
Ejemplo n.º 4
0
        public static void CreateURLRewriteRule(string serverFarm, bool forceRecreate = true)
        {
            using (ServerManager manager = new ServerManager())
            {
                ConfigurationElementCollection urlRewriterConfiguration = manager.GetApplicationHostConfiguration().GetSection("system.webServer/rewrite/globalRules").GetCollection();
                string str = string.Format("ARR_{0}_loadbalance", serverFarm);

                var attributes = new Dictionary <string, string>()
                {
                    { "name", str }
                };
                var urlRewriterRule = ConfigurationElementUtils.FindElement(urlRewriterConfiguration, "rule", attributes);

                if (urlRewriterRule != null)
                {
                    if (forceRecreate)
                    {
                        urlRewriterConfiguration.Remove(urlRewriterRule);
                    }
                    else
                    {
                        throw new InvalidOperationException("Cannot create rule with duplicate name");
                    }
                }
                ConfigurationElement element = urlRewriterConfiguration.CreateElement("rule");
                element["name"]           = str;
                element["patternSyntax"]  = "Wildcard";
                element["stopProcessing"] = true;
                element["enabled"]        = true;
                element.GetChildElement("match")["url"] = "*";
                ConfigurationElement childElement = element.GetChildElement("action");
                childElement["type"] = "Rewrite";
                childElement["url"]  = "http://" + serverFarm + "/{R:0}";
                urlRewriterConfiguration.Add(element);
                manager.CommitChanges();
            }
        }