// Spludlow Software // Copyright © Samuel P. Ludlow 2020 All Rights Reserved // Distributed under the terms of the GNU General Public License version 3 // Distributed WITHOUT ANY WARRANTY; without implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE // https://www.spludlow.co.uk/LICENCE.TXT // The Spludlow logo is a registered trademark of Samuel P. Ludlow and may not be used without permission // v1.14 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.Web.Administration; // C:\Windows\System32\inetsrv\Microsoft.Web.Administration.dll (Switch off copy local) using System.Data; using System.Collections; using System.IO; using System.Web; namespace Spludlow { public class WebServer { public static int ClearCache() { int count = 0; foreach (DictionaryEntry entry in HttpContext.Current.Cache) { HttpContext.Current.Cache.Remove((string)entry.Key); ++count; } return count; } public static string StateName(ObjectState objectState) { return Enum.GetName(typeof(ObjectState), objectState); } public static string Query(string applicationPoolName) { using (ServerManager serverManager = new ServerManager()) { ApplicationPool appPool = serverManager.ApplicationPools[applicationPoolName]; if (appPool == null) throw new ApplicationException("ApplicationPool Not Found:\t" + applicationPoolName); return StateName(appPool.State); } } public static DataSet QueryAll() { return Queries(null); } public static DataSet Queries(string[] applicationPoolNames) { DataTable table = Spludlow.Data.TextTable.ReadText(new string[] { "Host Name State Version", "String String String String", }); using (ServerManager serverManager = new ServerManager()) { if (applicationPoolNames == null) { List poolList = new List(); foreach (ApplicationPool applicationPool in serverManager.ApplicationPools) poolList.Add(applicationPool.Name); applicationPoolNames = poolList.ToArray(); } foreach (string applicationPoolName in applicationPoolNames) { ApplicationPool applicationPool = serverManager.ApplicationPools[applicationPoolName]; if (applicationPool == null) throw new ApplicationException("ApplicationPool Not Found:\t" + applicationPoolName); string state = StateName(applicationPool.State); table.Rows.Add(new object[] { Environment.MachineName, applicationPool.Name, state, applicationPool.ManagedRuntimeVersion }); } } return Spludlow.Data.ADO.WireDataSet(table); } public static void Recycle(string applicationPoolName) { Recycle(new string[] { applicationPoolName }); } public static void Recycle(string[] applicationPoolNames) { using (ServerManager serverManager = new ServerManager()) { foreach (string applicationPoolName in applicationPoolNames) { ApplicationPool applicationPool = serverManager.ApplicationPools[applicationPoolName]; if (applicationPool == null) throw new ApplicationException("ApplicationPool Not Found:\t" + applicationPoolName); applicationPool.Recycle(); } } } public static void Start(string applicationPoolName) { Start(new string[] { applicationPoolName }); } public static void Start(string[] applicationPoolNames) { using (ServerManager serverManager = new ServerManager()) { foreach (string applicationPoolName in applicationPoolNames) { ApplicationPool applicationPool = serverManager.ApplicationPools[applicationPoolName]; if (applicationPool == null) throw new ApplicationException("ApplicationPool Not Found:\t" + applicationPoolName); bool worked = false; for (int attempt = 1; attempt <= 10 && worked == false; ++attempt) { try { applicationPool.Start(); worked = true; } catch (System.Runtime.InteropServices.COMException ee) { if ((uint)ee.HResult != 0x80070425) // The service cannot accept control messages at this time. (Exception from HRESULT: 0x80070425) throw ee; Spludlow.Log.Warning("WebServer, Start Application Pool; Attempt:" + attempt + ", " + ee.Message, ee); System.Threading.Thread.Sleep(1000); } } if (worked == false) throw new ApplicationException("WebServer, Start Application Pool; failed all attempts"); } } } public static void Stop(string applicationPoolName) { Stop(new string[] { applicationPoolName }); } public static void Stop(string[] applicationPoolNames) { using (ServerManager serverManager = new ServerManager()) { foreach (string applicationPoolName in applicationPoolNames) { ApplicationPool applicationPool = serverManager.ApplicationPools[applicationPoolName]; if (applicationPool == null) throw new ApplicationException("ApplicationPool Not Found:\t" + applicationPoolName); applicationPool.Stop(); } } } public static string ServerConfig() // "C:\Program Files\SpludlowV1\Spludlow-Process\Spludlow-Process.exe" -A Spludlow -T Spludlow.WebServer -M ServerConfig -P IIS.txt { return ServerConfig(null); } public static string ServerConfig(string filename) { StringBuilder text = new StringBuilder(); using (ServerManager serverManager = new ServerManager()) { text.AppendLine("ApplicationPools:"); foreach (ApplicationPool pool in serverManager.ApplicationPools) { text.AppendLine(pool.Name + "\t" + pool.ProcessModel.UserName + "\t" + pool.ProcessModel.LoadUserProfile); } text.AppendLine(); text.AppendLine("Sites:"); foreach (Site site in serverManager.Sites) { StringBuilder bindings = new StringBuilder(); foreach (Binding binding in site.Bindings) { if (bindings.Length > 0) bindings.Append(", "); bindings.Append(binding.BindingInformation); } text.AppendLine(site.Name + "\t" + bindings.ToString()); foreach (Application app in site.Applications) { text.AppendLine("\t" + app.Path + "\t" + app.ApplicationPoolName); foreach (VirtualDirectory virtualDirectory in app.VirtualDirectories) text.AppendLine("\t\t" + virtualDirectory.Path + "\t" + virtualDirectory.PhysicalPath); } } text.AppendLine(); } string fileText = text.ToString(); if (filename != null) { File.WriteAllText(filename, fileText); } return fileText; } public static DataSet QuerySites() { DataTable table = Spludlow.Data.TextTable.ReadText(new string[] { "SiteId SiteName SiteState SiteLogFileDirectory", "Int64 String String String", }); using (ServerManager serverManager = new ServerManager()) { foreach (Site site in serverManager.Sites) { string name = ""; if (site.Name != null) name = site.Name; string state = ""; try { state = site.State.ToString(); } catch (Exception ee) { state = ee.Message; } string logFileDirectory = ""; if (site.LogFile != null && site.LogFile.Directory != null) logFileDirectory = Environment.ExpandEnvironmentVariables(site.LogFile.Directory); table.Rows.Add(site.Id, name, state, logFileDirectory); } } return Spludlow.Data.ADO.WireDataSet(table); } public static DataSet QueryApplicationPools() { DataTable table = Spludlow.Data.TextTable.ReadText(new string[] { "Name State", "String String", }); using (ServerManager serverManager = new ServerManager()) { foreach (ApplicationPool applicationPool in serverManager.ApplicationPools) { table.Rows.Add(applicationPool.Name, applicationPool.State.ToString()); } } return Spludlow.Data.ADO.WireDataSet(table); } } }