diff --git a/src/WinSW.Core/Configuration/DefaultSettings.cs b/src/WinSW.Core/Configuration/DefaultSettings.cs index 2f0e725..a55d21e 100644 --- a/src/WinSW.Core/Configuration/DefaultSettings.cs +++ b/src/WinSW.Core/Configuration/DefaultSettings.cs @@ -62,7 +62,7 @@ namespace WinSW.Configuration public bool Interactive => false; // Logging - public Log Log { get => new LogDefaults(); } + public Log Log => new LogDefaults(); public string LogDirectory => DefaultLogSettings.Directory; @@ -76,7 +76,7 @@ namespace WinSW.Configuration public string ErrFilePattern => this.Log.ErrFilePattern; - public ServiceAccount ServiceAccount => new ServiceAccount() + public ServiceAccount ServiceAccount => new() { ServiceAccountName = null, ServiceAccountDomain = null, @@ -108,13 +108,13 @@ namespace WinSW.Configuration public override int? Period => 1; - public override bool OutFileDisabled { get => false; } + public override bool OutFileDisabled => false; - public override bool ErrFileDisabled { get => false; } + public override bool ErrFileDisabled => false; - public override string OutFilePattern { get => ".out.log"; } + public override string OutFilePattern => ".out.log"; - public override string ErrFilePattern { get => ".err.log"; } + public override string ErrFilePattern => ".err.log"; public override string? AutoRollAtTime => null; @@ -125,9 +125,9 @@ namespace WinSW.Configuration } // Environment - public List Downloads => new List(0); + public List Downloads => new(0); - public Dictionary EnvironmentVariables => new Dictionary(0); + public Dictionary EnvironmentVariables => new(0); // Misc public bool BeepOnShutdown => false; @@ -135,7 +135,7 @@ namespace WinSW.Configuration // Extensions public XmlNode? ExtensionsConfiguration => null; - public List? YamlExtensionsConfiguration => new List(0); + public List? YamlExtensionsConfiguration => new(0); public string BaseName { @@ -160,7 +160,7 @@ namespace WinSW.Configuration } } - public List ExtensionIds => new List(0); + public List ExtensionIds => new(0); public string? SecurityDescriptor => null; } diff --git a/src/WinSW.Core/Configuration/Log.cs b/src/WinSW.Core/Configuration/Log.cs index a73dff6..dc98249 100644 --- a/src/WinSW.Core/Configuration/Log.cs +++ b/src/WinSW.Core/Configuration/Log.cs @@ -78,7 +78,7 @@ namespace WinSW.Configuration if (this.AutoRollAtTime != null) { // validate it - if (!TimeSpan.TryParse(this.AutoRollAtTime, out TimeSpan autoRollAtTimeValue)) + if (!TimeSpan.TryParse(this.AutoRollAtTime, out var autoRollAtTimeValue)) { throw new InvalidDataException("Roll-Size-Time Based rolling policy is specified but autoRollAtTime does not match the TimeSpan format HH:mm:ss found in configuration XML."); } diff --git a/src/WinSW.Core/Configuration/YamlConfiguration.cs b/src/WinSW.Core/Configuration/YamlConfiguration.cs index 4f6432b..3226bce 100644 --- a/src/WinSW.Core/Configuration/YamlConfiguration.cs +++ b/src/WinSW.Core/Configuration/YamlConfiguration.cs @@ -287,10 +287,8 @@ namespace WinSW.Configuration { if (!int.TryParse(this.ZipOlderThanNumDaysYamlLog, out int zipolderthannumdaysValue)) { -// FIXME: Remove the build env specific warning suppression from the codebase -#pragma warning disable S2372 // Exceptions should not be thrown from property getters + // FIXME: Remove the build env specific warning suppression from the codebase throw new InvalidDataException("Roll-Size-Time Based rolling policy is specified but zipOlderThanNumDays does not match the int format found in configuration XML."); -#pragma warning restore S2372 // Exceptions should not be thrown from property getters } zipolderthannumdays = zipolderthannumdaysValue; @@ -356,7 +354,7 @@ namespace WinSW.Configuration return AuthType.None; } - var auth = ExpandEnv(this.AuthYamlDownload); + string auth = ExpandEnv(this.AuthYamlDownload); try { @@ -388,7 +386,7 @@ namespace WinSW.Configuration { get { - SC_ACTION_TYPE actionType = this.FailureAction switch + var actionType = this.FailureAction switch { "restart" => SC_ACTION_TYPE.SC_ACTION_RESTART, "none" => SC_ACTION_TYPE.SC_ACTION_NONE, @@ -493,7 +491,7 @@ namespace WinSW.Configuration return this.Defaults.StartMode; } - var p = ExpandEnv(this.StartModeYaml); + string p = ExpandEnv(this.StartModeYaml); try { @@ -516,7 +514,7 @@ namespace WinSW.Configuration { get { - var args = this.GetArguments(this.ArgumentsYaml, ArgType.Arg); + string? args = this.GetArguments(this.ArgumentsYaml, ArgType.Arg); return args is null ? this.Defaults.Arguments : args; } } @@ -572,7 +570,7 @@ namespace WinSW.Configuration return this.Defaults.Priority; } - var p = ExpandEnv(this.PriorityYaml); + string p = ExpandEnv(this.PriorityYaml); try { @@ -604,7 +602,7 @@ namespace WinSW.Configuration var result = new List(0); - foreach (var item in this.ServiceDependenciesYaml) + foreach (string item in this.ServiceDependenciesYaml) { result.Add(ExpandEnv(item)); } @@ -638,8 +636,8 @@ namespace WinSW.Configuration continue; } - var key = item.Name; - var value = ExpandEnv(item.Value); + string key = item.Name; + string value = ExpandEnv(item.Value); this.EnvironmentVariables[key] = value; Environment.SetEnvironmentVariable(key, value); diff --git a/src/WinSW.Core/Download.cs b/src/WinSW.Core/Download.cs index 9d2b276..4eb04a8 100644 --- a/src/WinSW.Core/Download.cs +++ b/src/WinSW.Core/Download.cs @@ -150,10 +150,10 @@ namespace WinSW public void Perform() #endif { - WebRequest request = WebRequest.Create(this.From); + var request = WebRequest.Create(this.From); if (!string.IsNullOrEmpty(this.Proxy)) { - CustomProxyInformation proxyInformation = new CustomProxyInformation(this.Proxy!); + var proxyInformation = new CustomProxyInformation(this.Proxy!); if (proxyInformation.Credentials != null) { request.Proxy = new WebProxy(proxyInformation.ServerAddress, false, null, proxyInformation.Credentials); @@ -196,12 +196,12 @@ namespace WinSW try { #if VNEXT - using (WebResponse response = await request.GetResponseAsync()) + using (var response = await request.GetResponseAsync()) #else - using (WebResponse response = request.GetResponse()) + using (var response = request.GetResponse()) #endif - using (Stream responseStream = response.GetResponseStream()) - using (FileStream tmpStream = new FileStream(tmpFilePath, FileMode.Create)) + using (var responseStream = response.GetResponseStream()) + using (var tmpStream = new FileStream(tmpFilePath, FileMode.Create)) { if (supportsIfModifiedSince) { diff --git a/src/WinSW.Core/DynamicProxy.cs b/src/WinSW.Core/DynamicProxy.cs index 283d758..9d7ec5a 100644 --- a/src/WinSW.Core/DynamicProxy.cs +++ b/src/WinSW.Core/DynamicProxy.cs @@ -27,7 +27,7 @@ namespace DynamicProxy private const string ModuleName = "ProxyModule"; private const string HandlerName = "handler"; - private static readonly Dictionary TypeCache = new Dictionary(); + private static readonly Dictionary TypeCache = new(); private static readonly AssemblyBuilder AssemblyBuilder = #if VNEXT @@ -57,26 +57,26 @@ namespace DynamicProxy private static Type CreateType(string dynamicTypeName, Type[] interfaces) { - Type objType = typeof(object); - Type handlerType = typeof(IProxyInvocationHandler); + var objType = typeof(object); + var handlerType = typeof(IProxyInvocationHandler); - TypeAttributes typeAttributes = TypeAttributes.Public | TypeAttributes.Sealed; + var typeAttributes = TypeAttributes.Public | TypeAttributes.Sealed; // Gather up the proxy information and create a new type builder. One that // inherits from Object and implements the interface passed in - TypeBuilder typeBuilder = ModuleBuilder.DefineType( + var typeBuilder = ModuleBuilder.DefineType( dynamicTypeName, typeAttributes, objType, interfaces); // Define a member variable to hold the delegate - FieldBuilder handlerField = typeBuilder.DefineField( + var handlerField = typeBuilder.DefineField( HandlerName, handlerType, FieldAttributes.Private | FieldAttributes.InitOnly); // build a constructor that takes the delegate object as the only argument - ConstructorInfo baseConstructor = objType.GetConstructor(Type.EmptyTypes)!; - ConstructorBuilder delegateConstructor = typeBuilder.DefineConstructor( + var baseConstructor = objType.GetConstructor(Type.EmptyTypes)!; + var delegateConstructor = typeBuilder.DefineConstructor( MethodAttributes.Public, CallingConventions.Standard, new Type[] { handlerType }); - ILGenerator constructorIL = delegateConstructor.GetILGenerator(); + var constructorIL = delegateConstructor.GetILGenerator(); // Load "this" constructorIL.Emit(OpCodes.Ldarg_0); @@ -98,7 +98,7 @@ namespace DynamicProxy // for every method that the interfaces define, build a corresponding // method in the dynamic type that calls the handlers invoke method. - foreach (Type interfaceType in interfaces) + foreach (var interfaceType in interfaces) { GenerateMethod(interfaceType, handlerField, typeBuilder); } @@ -118,17 +118,17 @@ namespace DynamicProxy private static void GenerateMethod(Type interfaceType, FieldBuilder handlerField, TypeBuilder typeBuilder) { - MethodInfo[] interfaceMethods = interfaceType.GetMethods(); + var interfaceMethods = interfaceType.GetMethods(); for (int i = 0; i < interfaceMethods.Length; i++) { - MethodInfo methodInfo = interfaceMethods[i]; + var methodInfo = interfaceMethods[i]; // Get the method parameters since we need to create an array // of parameter types - ParameterInfo[] methodParams = methodInfo.GetParameters(); + var methodParams = methodInfo.GetParameters(); int numOfParams = methodParams.Length; - Type[] methodParameters = new Type[numOfParams]; + var methodParameters = new Type[numOfParams]; // convert the ParameterInfo objects into Type for (int j = 0; j < numOfParams; j++) @@ -137,14 +137,14 @@ namespace DynamicProxy } // create a new builder for the method in the interface - MethodBuilder methodBuilder = typeBuilder.DefineMethod( + var methodBuilder = typeBuilder.DefineMethod( methodInfo.Name, /*MethodAttributes.Public | MethodAttributes.Virtual | */ methodInfo.Attributes & ~MethodAttributes.Abstract, CallingConventions.Standard, methodInfo.ReturnType, methodParameters); - ILGenerator methodIL = methodBuilder.GetILGenerator(); + var methodIL = methodBuilder.GetILGenerator(); // invoke target: IProxyInvocationHandler methodIL.Emit(OpCodes.Ldarg_0); @@ -196,7 +196,7 @@ namespace DynamicProxy } // Iterate through the parent interfaces and recursively call this method - foreach (Type parentType in interfaceType.GetInterfaces()) + foreach (var parentType in interfaceType.GetInterfaces()) { GenerateMethod(parentType, handlerField, typeBuilder); } diff --git a/src/WinSW.Core/Extensions/WinSWExtensionManager.cs b/src/WinSW.Core/Extensions/WinSWExtensionManager.cs index 686dab7..a81f168 100644 --- a/src/WinSW.Core/Extensions/WinSWExtensionManager.cs +++ b/src/WinSW.Core/Extensions/WinSWExtensionManager.cs @@ -140,8 +140,8 @@ namespace WinSW.Extensions private void LoadExtensionFromXml(string id) { - XmlNode? extensionsConfig = this.ServiceDescriptor.ExtensionsConfiguration; - XmlElement? configNode = extensionsConfig is null ? null : extensionsConfig.SelectSingleNode("extension[@id='" + id + "'][1]") as XmlElement; + var extensionsConfig = this.ServiceDescriptor.ExtensionsConfiguration; + var configNode = extensionsConfig is null ? null : extensionsConfig.SelectSingleNode("extension[@id='" + id + "'][1]") as XmlElement; if (configNode is null) { throw new ExtensionException(id, "Cannot get the configuration entry"); @@ -151,7 +151,7 @@ namespace WinSW.Extensions if (descriptor.Enabled) { - IWinSWExtension extension = this.CreateExtensionInstance(descriptor.Id, descriptor.ClassName); + var extension = this.CreateExtensionInstance(descriptor.Id, descriptor.ClassName); extension.Descriptor = descriptor; try { @@ -187,7 +187,7 @@ namespace WinSW.Extensions if (descriptor.Enabled) { - IWinSWExtension extension = this.CreateExtensionInstance(descriptor.Id, descriptor.ClassName); + var extension = this.CreateExtensionInstance(descriptor.Id, descriptor.ClassName); extension.Descriptor = descriptor; try @@ -237,7 +237,7 @@ namespace WinSW.Extensions className = "WinSW.Plugins.SharedDirectoryMapper"; } - Type? t = Type.GetType(className); + var t = Type.GetType(className); if (t is null) { throw new ExtensionException(id, "Class " + className + " does not exist"); diff --git a/src/WinSW.Core/LogAppenders.cs b/src/WinSW.Core/LogAppenders.cs index e804c2c..d8fb2b6 100644 --- a/src/WinSW.Core/LogAppenders.cs +++ b/src/WinSW.Core/LogAppenders.cs @@ -107,7 +107,7 @@ namespace WinSW } } - protected StreamWriter CreateWriter(FileStream stream) => new StreamWriter(stream) { AutoFlush = true }; + protected StreamWriter CreateWriter(FileStream stream) => new(stream) { AutoFlush = true }; protected abstract void LogOutput(StreamReader outputReader); @@ -200,10 +200,10 @@ namespace WinSW /// private void CopyStreamWithDateRotation(StreamReader reader, string ext) { - PeriodicRollingCalendar periodicRollingCalendar = new PeriodicRollingCalendar(this.Pattern, this.Period); + var periodicRollingCalendar = new PeriodicRollingCalendar(this.Pattern, this.Period); periodicRollingCalendar.Init(); - StreamWriter writer = this.CreateWriter(new FileStream(this.BaseLogFileName + "_" + periodicRollingCalendar.Format + ext, FileMode.Append)); + var writer = this.CreateWriter(new FileStream(this.BaseLogFileName + "_" + periodicRollingCalendar.Format + ext, FileMode.Append)); string? line; while ((line = reader.ReadLine()) != null) { @@ -259,7 +259,7 @@ namespace WinSW /// private void CopyStreamWithRotation(StreamReader reader, string ext) { - StreamWriter writer = this.CreateWriter(new FileStream(this.BaseLogFileName + ext, FileMode.Append)); + var writer = this.CreateWriter(new FileStream(this.BaseLogFileName + ext, FileMode.Append)); long fileLength = new FileInfo(this.BaseLogFileName + ext).Length; string? line; @@ -383,20 +383,20 @@ namespace WinSW private void CopyStreamWithRotation(StreamReader reader, string extension) { // lock required as the timer thread and the thread that will write to the stream could try and access the file stream at the same time - var fileLock = new object(); + object fileLock = new object(); - var baseDirectory = Path.GetDirectoryName(this.BaseLogFileName)!; - var baseFileName = Path.GetFileName(this.BaseLogFileName); - var logFile = this.BaseLogFileName + extension; + string baseDirectory = Path.GetDirectoryName(this.BaseLogFileName)!; + string baseFileName = Path.GetFileName(this.BaseLogFileName); + string logFile = this.BaseLogFileName + extension; var writer = this.CreateWriter(new FileStream(logFile, FileMode.Append)); - var fileLength = new FileInfo(logFile).Length; + long fileLength = new FileInfo(logFile).Length; // We auto roll at time is configured then we need to create a timer and wait until time is elasped and roll the file over if (this.AutoRollAtTime is TimeSpan autoRollAtTime) { // Run at start - var tickTime = this.SetupRollTimer(autoRollAtTime); + double tickTime = this.SetupRollTimer(autoRollAtTime); var timer = new System.Timers.Timer(tickTime); timer.Elapsed += (s, e) => { @@ -408,8 +408,8 @@ namespace WinSW writer.Dispose(); var now = DateTime.Now.AddDays(-1); - var nextFileNumber = this.GetNextFileNumber(extension, baseDirectory, baseFileName, now); - var nextFileName = Path.Combine(baseDirectory, string.Format("{0}.{1}.#{2:D4}{3}", baseFileName, now.ToString(this.FilePattern), nextFileNumber, extension)); + int nextFileNumber = this.GetNextFileNumber(extension, baseDirectory, baseFileName, now); + string nextFileName = Path.Combine(baseDirectory, string.Format("{0}.{1}.#{2:D4}{3}", baseFileName, now.ToString(this.FilePattern), nextFileNumber, extension)); File.Move(logFile, nextFileName); writer = this.CreateWriter(new FileStream(logFile, FileMode.Create)); @@ -445,8 +445,8 @@ namespace WinSW { // roll file var now = DateTime.Now; - var nextFileNumber = this.GetNextFileNumber(extension, baseDirectory, baseFileName, now); - var nextFileName = Path.Combine( + int nextFileNumber = this.GetNextFileNumber(extension, baseDirectory, baseFileName, now); + string nextFileName = Path.Combine( baseDirectory, string.Format("{0}.{1}.#{2:D4}{3}", baseFileName, now.ToString(this.FilePattern), nextFileNumber, extension)); File.Move(logFile, nextFileName); @@ -574,21 +574,21 @@ namespace WinSW private int GetNextFileNumber(string ext, string baseDirectory, string baseFileName, DateTime now) { - var nextFileNumber = 0; - var files = Directory.GetFiles(baseDirectory, string.Format("{0}.{1}.#*{2}", baseFileName, now.ToString(this.FilePattern), ext)); + int nextFileNumber = 0; + string[] files = Directory.GetFiles(baseDirectory, string.Format("{0}.{1}.#*{2}", baseFileName, now.ToString(this.FilePattern), ext)); if (files.Length == 0) { nextFileNumber = 1; } else { - foreach (var f in files) + foreach (string f in files) { try { - var filenameOnly = Path.GetFileNameWithoutExtension(f); - var hashIndex = filenameOnly.IndexOf('#'); - var lastNumberAsString = filenameOnly.Substring(hashIndex + 1, 4); + string filenameOnly = Path.GetFileNameWithoutExtension(f); + int hashIndex = filenameOnly.IndexOf('#'); + string lastNumberAsString = filenameOnly.Substring(hashIndex + 1, 4); if (int.TryParse(lastNumberAsString, out int lastNumber)) { if (lastNumber > nextFileNumber) diff --git a/src/WinSW.Core/Logging/ServiceEventLogAppender.cs b/src/WinSW.Core/Logging/ServiceEventLogAppender.cs index 34b99ab..d2c1860 100644 --- a/src/WinSW.Core/Logging/ServiceEventLogAppender.cs +++ b/src/WinSW.Core/Logging/ServiceEventLogAppender.cs @@ -16,7 +16,7 @@ namespace WinSW.Logging protected override void Append(LoggingEvent loggingEvent) { - EventLog? eventLog = this.Provider.Locate(); + var eventLog = this.Provider.Locate(); // We write the event iff the provider is ready eventLog?.WriteEntry(loggingEvent.RenderedMessage, ToEventLogEntryType(loggingEvent.Level)); diff --git a/src/WinSW.Core/Native/Security.cs b/src/WinSW.Core/Native/Security.cs index 5ca0aea..92f51a8 100644 --- a/src/WinSW.Core/Native/Security.cs +++ b/src/WinSW.Core/Native/Security.cs @@ -9,7 +9,7 @@ namespace WinSW.Native { internal static void AddServiceLogonRight(string domain, string user) { - IntPtr sid = GetAccountSid(domain, user); + var sid = GetAccountSid(domain, user); try { @@ -34,8 +34,8 @@ namespace WinSW.Native string accountName = domain + "\\" + user; _ = LookupAccountName(null, accountName, IntPtr.Zero, ref sidSize, IntPtr.Zero, ref domainNameLength, out _); - IntPtr sid = Marshal.AllocHGlobal(sidSize); - IntPtr domainName = Marshal.AllocHGlobal(domainNameLength * sizeof(char)); + var sid = Marshal.AllocHGlobal(sidSize); + var domainName = Marshal.AllocHGlobal(domainNameLength * sizeof(char)); try { @@ -54,7 +54,7 @@ namespace WinSW.Native private static void AddAccountRight(IntPtr sid, string rightName) { - uint status = LsaOpenPolicy(IntPtr.Zero, default, PolicyAccess.ALL_ACCESS, out IntPtr policyHandle); + uint status = LsaOpenPolicy(IntPtr.Zero, default, PolicyAccess.ALL_ACCESS, out var policyHandle); if (status != 0) { throw new Win32Exception(LsaNtStatusToWinError(status)); @@ -62,7 +62,7 @@ namespace WinSW.Native try { - LSA_UNICODE_STRING userRight = new LSA_UNICODE_STRING + var userRight = new LSA_UNICODE_STRING { Buffer = rightName, Length = (ushort)(rightName.Length * sizeof(char)), diff --git a/src/WinSW.Core/Native/Service.cs b/src/WinSW.Core/Native/Service.cs index d5e3f5e..cdc8692 100644 --- a/src/WinSW.Core/Native/Service.cs +++ b/src/WinSW.Core/Native/Service.cs @@ -54,7 +54,7 @@ namespace WinSW.Native internal static ServiceManager Open() { - IntPtr handle = OpenSCManager(null, null, ServiceManagerAccess.ALL_ACCESS); + var handle = OpenSCManager(null, null, ServiceManagerAccess.ALL_ACCESS); if (handle == IntPtr.Zero) { Throw.Win32Exception("Failed to open the service control manager database."); @@ -65,7 +65,7 @@ namespace WinSW.Native internal Service OpenService(string serviceName) { - IntPtr serviceHandle = ServiceApis.OpenService(this.handle, serviceName, ServiceAccess.ALL_ACCESS); + var serviceHandle = ServiceApis.OpenService(this.handle, serviceName, ServiceAccess.ALL_ACCESS); if (serviceHandle == IntPtr.Zero) { Throw.Win32Exception("Failed to open the service."); diff --git a/src/WinSW.Core/Native/Throw.cs b/src/WinSW.Core/Native/Throw.cs index b92689d..7fe07d4 100644 --- a/src/WinSW.Core/Native/Throw.cs +++ b/src/WinSW.Core/Native/Throw.cs @@ -7,7 +7,7 @@ namespace WinSW.Native { internal static void Win32Exception(string message) { - Win32Exception inner = new Win32Exception(); + var inner = new Win32Exception(); Debug.Assert(inner.NativeErrorCode != 0); Debug.Assert(message.EndsWith(".")); throw new Win32Exception(inner.NativeErrorCode, message + ' ' + inner.Message); diff --git a/src/WinSW.Core/PeriodicRollingCalendar.cs b/src/WinSW.Core/PeriodicRollingCalendar.cs index 03a84f6..df6820d 100644 --- a/src/WinSW.Core/PeriodicRollingCalendar.cs +++ b/src/WinSW.Core/PeriodicRollingCalendar.cs @@ -40,15 +40,15 @@ namespace WinSW private Periodicity DeterminePeriodicityType() { - PeriodicRollingCalendar periodicRollingCalendar = new PeriodicRollingCalendar(this.format, this.period); - DateTime epoch = new DateTime(1970, 1, 1); + var periodicRollingCalendar = new PeriodicRollingCalendar(this.format, this.period); + var epoch = new DateTime(1970, 1, 1); - foreach (Periodicity i in ValidOrderedList) + foreach (var i in ValidOrderedList) { string r0 = epoch.ToString(this.format); periodicRollingCalendar.PeriodicityType = i; - DateTime next = periodicRollingCalendar.NextTriggeringTime(epoch, 1); + var next = periodicRollingCalendar.NextTriggeringTime(epoch, 1); string r1 = next.ToString(this.format); if (r0 != r1) @@ -91,7 +91,7 @@ namespace WinSW { get { - DateTime now = DateTime.Now; + var now = DateTime.Now; if (now > this.nextRoll) { this.currentRoll = now; diff --git a/src/WinSW.Core/ServiceDescriptor.cs b/src/WinSW.Core/ServiceDescriptor.cs index c4ca6b4..3216760 100644 --- a/src/WinSW.Core/ServiceDescriptor.cs +++ b/src/WinSW.Core/ServiceDescriptor.cs @@ -16,10 +16,7 @@ namespace WinSW /// public class ServiceDescriptor : IWinSWConfiguration { -#pragma warning disable S2755 // XML parsers should not be vulnerable to XXE attacks - protected readonly XmlDocument dom = new XmlDocument(); -#pragma warning restore S2755 // XML parsers should not be vulnerable to XXE attacks - + protected readonly XmlDocument dom = new(); private readonly Dictionary environmentVariables; public static DefaultWinSWSettings Defaults { get; } = new DefaultWinSWSettings(); @@ -84,9 +81,7 @@ namespace WinSW public static ServiceDescriptor FromXML(string xml) { -#pragma warning disable S2755 // XML parsers should not be vulnerable to XXE attacks var xmlDom = new XmlDocument(); -#pragma warning restore S2755 // XML parsers should not be vulnerable to XXE attacks xmlDom.LoadXml(xml); return new ServiceDescriptor(xmlDom); } @@ -98,7 +93,7 @@ namespace WinSW private string? SingleElement(string tagName, bool optional) { - XmlNode? n = this.dom.SelectSingleNode("//" + tagName); + var n = this.dom.SelectSingleNode("//" + tagName); if (n is null && !optional) { throw new InvalidDataException("<" + tagName + "> is missing in configuration XML"); @@ -109,14 +104,14 @@ namespace WinSW private bool SingleBoolElement(string tagName, bool defaultValue) { - XmlNode? e = this.dom.SelectSingleNode("//" + tagName); + var e = this.dom.SelectSingleNode("//" + tagName); return e is null ? defaultValue : bool.Parse(e.InnerText); } private int SingleIntElement(XmlNode parent, string tagName, int defaultValue) { - XmlNode? e = parent.SelectSingleNode(tagName); + var e = parent.SelectSingleNode(tagName); return e is null ? defaultValue : int.Parse(e.InnerText); } @@ -153,7 +148,7 @@ namespace WinSW return arguments; } - XmlNode? argumentsNode = this.dom.SelectSingleNode("//arguments"); + var argumentsNode = this.dom.SelectSingleNode("//arguments"); return argumentsNode is null ? Defaults.Arguments : Environment.ExpandEnvironmentVariables(argumentsNode.InnerText); } @@ -173,7 +168,7 @@ namespace WinSW return startArguments; } - XmlNode? startArgumentsNode = this.dom.SelectSingleNode("//startarguments"); + var startArgumentsNode = this.dom.SelectSingleNode("//startarguments"); return startArgumentsNode is null ? null : Environment.ExpandEnvironmentVariables(startArgumentsNode.InnerText); } @@ -193,7 +188,7 @@ namespace WinSW return stopArguments; } - XmlNode? stopArgumentsNode = this.dom.SelectSingleNode("//stoparguments"); + var stopArgumentsNode = this.dom.SelectSingleNode("//stoparguments"); return stopArgumentsNode is null ? null : Environment.ExpandEnvironmentVariables(stopArgumentsNode.InnerText); } @@ -203,7 +198,7 @@ namespace WinSW { get { - var wd = this.SingleElement("workingdirectory", true); + string? wd = this.SingleElement("workingdirectory", true); return string.IsNullOrEmpty(wd) ? Defaults.WorkingDirectory : wd!; } } @@ -212,14 +207,14 @@ namespace WinSW { get { - XmlNode? argumentNode = this.ExtensionsConfiguration; - XmlNodeList? extensions = argumentNode?.SelectNodes("extension"); + var argumentNode = this.ExtensionsConfiguration; + var extensions = argumentNode?.SelectNodes("extension"); if (extensions is null) { return new List(0); } - List result = new List(extensions.Count); + var result = new List(extensions.Count); for (int i = 0; i < extensions.Count; i++) { result.Add(XmlHelper.SingleAttribute((XmlElement)extensions[i]!, "id")); @@ -237,15 +232,15 @@ namespace WinSW /// private string? AppendTags(string tagName, string? defaultValue = null) { - XmlNode? argumentNode = this.dom.SelectSingleNode("//" + tagName); + var argumentNode = this.dom.SelectSingleNode("//" + tagName); if (argumentNode is null) { return defaultValue; } - StringBuilder arguments = new StringBuilder(); + var arguments = new StringBuilder(); - XmlNodeList argumentNodeList = this.dom.SelectNodes("//" + tagName)!; + var argumentNodeList = this.dom.SelectNodes("//" + tagName)!; for (int i = 0; i < argumentNodeList.Count; i++) { arguments.Append(' '); @@ -276,7 +271,7 @@ namespace WinSW /// /// LogDirectory is the service wrapper executable directory or the optionally specified logpath element. /// - public string LogDirectory { get => this.Log.Directory; } + public string LogDirectory => this.Log.Directory; public string LogMode { @@ -285,7 +280,7 @@ namespace WinSW string? mode = null; // first, backward compatibility with older configuration - XmlElement? e = (XmlElement?)this.dom.SelectSingleNode("//logmode"); + var e = (XmlElement?)this.dom.SelectSingleNode("//logmode"); if (e != null) { mode = e.InnerText; @@ -308,19 +303,13 @@ namespace WinSW { get { - XmlNode? loggingName = this.dom.SelectSingleNode("//logname"); + var loggingName = this.dom.SelectSingleNode("//logname"); return loggingName is null ? this.BaseName : Environment.ExpandEnvironmentVariables(loggingName.InnerText); } } - public Log Log - { - get - { - return new XmlLogSettings(this); - } - } + public Log Log => new XmlLogSettings(this); private class XmlLogSettings : Log { @@ -335,7 +324,7 @@ namespace WinSW { get { - XmlElement? e = (XmlElement?)this.d.dom.SelectSingleNode("//logmode"); + var e = (XmlElement?)this.d.dom.SelectSingleNode("//logmode"); // this is more modern way, to support nested elements as configuration e ??= (XmlElement?)this.d.dom.SelectSingleNode("//log")!; // WARNING: NRE @@ -343,15 +332,15 @@ namespace WinSW } } - public override string? Mode { get => this.d.LogMode; } + public override string? Mode => this.d.LogMode; - public override string Name { get => this.d.LogName; } + public override string Name => this.d.LogName; public override string Directory { get { - XmlNode? loggingNode = this.d.dom.SelectSingleNode("//logpath"); + var loggingNode = this.d.dom.SelectSingleNode("//logpath"); return loggingNode is null ? Defaults.LogDirectory @@ -359,22 +348,20 @@ namespace WinSW } } - public override int? SizeThreshold { get => this.d.SingleIntElement(this.E, "sizeThreshold", 10 * 1024); } + public override int? SizeThreshold => this.d.SingleIntElement(this.E, "sizeThreshold", 10 * 1024); - public override int? KeepFiles { get => this.d.SingleIntElement(this.E, "keepFiles", SizeBasedRollingLogAppender.DefaultFilesToKeep); } + public override int? KeepFiles => this.d.SingleIntElement(this.E, "keepFiles", SizeBasedRollingLogAppender.DefaultFilesToKeep); - public override int? Period { get => this.d.SingleIntElement(this.E, "period", 1); } + public override int? Period => this.d.SingleIntElement(this.E, "period", 1); public override string Pattern { get { - XmlNode? patternNode = this.E.SelectSingleNode("pattern"); + var patternNode = this.E.SelectSingleNode("pattern"); if (patternNode is null) { -#pragma warning disable S2372 // Exceptions should not be thrown from property getters throw new InvalidDataException("Time Based rolling policy is specified but no pattern can be found in configuration XML."); -#pragma warning restore S2372 // Exceptions should not be thrown from property getters } return patternNode.InnerText; @@ -389,7 +376,7 @@ namespace WinSW { get { - XmlNode? loggingName = this.d.dom.SelectSingleNode("//outfilepattern"); + var loggingName = this.d.dom.SelectSingleNode("//outfilepattern"); return loggingName is null ? Defaults.OutFilePattern : Environment.ExpandEnvironmentVariables(loggingName.InnerText); } @@ -399,7 +386,7 @@ namespace WinSW { get { - XmlNode? loggingName = this.d.dom.SelectSingleNode("//errfilepattern"); + var loggingName = this.d.dom.SelectSingleNode("//errfilepattern"); return loggingName is null ? Defaults.ErrFilePattern : Environment.ExpandEnvironmentVariables(loggingName.InnerText); } @@ -409,7 +396,7 @@ namespace WinSW { get { - XmlNode? autoRollAtTimeNode = this.E.SelectSingleNode("autoRollAtTime"); + var autoRollAtTimeNode = this.E.SelectSingleNode("autoRollAtTime"); return autoRollAtTimeNode?.InnerText; } } @@ -418,16 +405,14 @@ namespace WinSW { get { - XmlNode? zipolderthannumdaysNode = this.E.SelectSingleNode("zipOlderThanNumDays"); + var zipolderthannumdaysNode = this.E.SelectSingleNode("zipOlderThanNumDays"); int? zipolderthannumdays = null; if (zipolderthannumdaysNode != null) { // validate it if (!int.TryParse(zipolderthannumdaysNode.InnerText, out int zipolderthannumdaysValue)) { -#pragma warning disable S2372 // Exceptions should not be thrown from property getters throw new InvalidDataException("Roll-Size-Time Based rolling policy is specified but zipOlderThanNumDays does not match the int format found in configuration XML."); -#pragma warning restore S2372 // Exceptions should not be thrown from property getters } zipolderthannumdays = zipolderthannumdaysValue; @@ -441,7 +426,7 @@ namespace WinSW { get { - XmlNode? zipdateformatNode = this.E.SelectSingleNode("zipDateFormat"); + var zipdateformatNode = this.E.SelectSingleNode("zipDateFormat"); return zipdateformatNode is null ? null : zipdateformatNode.InnerText; } } @@ -454,7 +439,7 @@ namespace WinSW { get { - XmlNodeList? nodeList = this.dom.SelectNodes("//depend"); + var nodeList = this.dom.SelectNodes("//depend"); if (nodeList is null) { return Defaults.ServiceDependencies; @@ -540,7 +525,7 @@ namespace WinSW /// /// Environment variable overrides /// - public Dictionary EnvironmentVariables => new Dictionary(this.environmentVariables); + public Dictionary EnvironmentVariables => new(this.environmentVariables); /// /// List of downloads to be performed by the wrapper before starting @@ -550,13 +535,13 @@ namespace WinSW { get { - XmlNodeList? nodeList = this.dom.SelectNodes("//download"); + var nodeList = this.dom.SelectNodes("//download"); if (nodeList is null) { return Defaults.Downloads; } - List result = new List(nodeList.Count); + var result = new List(nodeList.Count); for (int i = 0; i < nodeList.Count; i++) { if (nodeList[i] is XmlElement element) @@ -573,25 +558,25 @@ namespace WinSW { get { - XmlNodeList? childNodes = this.dom.SelectNodes("//onfailure"); + var childNodes = this.dom.SelectNodes("//onfailure"); if (childNodes is null) { return new SC_ACTION[0]; } - SC_ACTION[] result = new SC_ACTION[childNodes.Count]; + var result = new SC_ACTION[childNodes.Count]; for (int i = 0; i < childNodes.Count; i++) { - XmlNode node = childNodes[i]!; + var node = childNodes[i]!; string action = node.Attributes!["action"]?.Value ?? throw new InvalidDataException("'action' is missing"); - SC_ACTION_TYPE type = action switch + var type = action switch { "restart" => SC_ACTION_TYPE.SC_ACTION_RESTART, "none" => SC_ACTION_TYPE.SC_ACTION_NONE, "reboot" => SC_ACTION_TYPE.SC_ACTION_REBOOT, _ => throw new Exception("Invalid failure action: " + action) }; - XmlAttribute? delay = node.Attributes["delay"]; + var delay = node.Attributes["delay"]; result[i] = new SC_ACTION(type, delay != null ? ConfigHelper.ParseTimeSpan(delay.Value) : TimeSpan.Zero); } @@ -603,7 +588,7 @@ namespace WinSW protected string? GetServiceAccountPart(XmlNode node, string subNodeName) { - XmlNode? subNode = node.SelectSingleNode(subNodeName); + var subNode = node.SelectSingleNode(subNodeName); if (subNode != null) { return subNode.InnerText; @@ -626,7 +611,7 @@ namespace WinSW { get { - XmlNode? node = this.dom.SelectSingleNode("//serviceaccount"); + var node = this.dom.SelectSingleNode("//serviceaccount"); if (node is null) { @@ -641,7 +626,7 @@ namespace WinSW serviceAccount.ServiceAccountPassword = this.GetServiceAccountPart(node, "password"); - var loginRight = this.GetServiceAccountPart(node, "allowservicelogon"); + string? loginRight = this.GetServiceAccountPart(node, "allowservicelogon"); serviceAccount.AllowServiceAcountLogonRight = this.ParseAllowServiceAcountLogonRight(loginRight); @@ -658,7 +643,7 @@ namespace WinSW { get { - var value = this.SingleElement("stopparentprocessfirst", true); + string? value = this.SingleElement("stopparentprocessfirst", true); if (bool.TryParse(value, out bool result)) { return result; @@ -689,11 +674,11 @@ namespace WinSW private Dictionary LoadEnvironmentVariables() { - XmlNodeList nodeList = this.dom.SelectNodes("//env")!; - Dictionary environment = new Dictionary(nodeList.Count); + var nodeList = this.dom.SelectNodes("//env")!; + var environment = new Dictionary(nodeList.Count); for (int i = 0; i < nodeList.Count; i++) { - XmlNode node = nodeList[i]!; + var node = nodeList[i]!; string key = node.Attributes!["name"]?.Value ?? throw new InvalidDataException("'name' is missing"); string value = Environment.ExpandEnvironmentVariables(node.Attributes["value"]?.Value ?? throw new InvalidDataException("'value' is missing")); environment[key] = value; diff --git a/src/WinSW.Core/ServiceDescriptorYaml.cs b/src/WinSW.Core/ServiceDescriptorYaml.cs index d731879..e982979 100644 --- a/src/WinSW.Core/ServiceDescriptorYaml.cs +++ b/src/WinSW.Core/ServiceDescriptorYaml.cs @@ -13,11 +13,11 @@ namespace WinSW public ServiceDescriptorYaml(string baseName, DirectoryInfo d) { - var basepath = Path.Combine(d.FullName, baseName); + string basepath = Path.Combine(d.FullName, baseName); using (var reader = new StreamReader(basepath + ".yml")) { - var file = reader.ReadToEnd(); + string file = reader.ReadToEnd(); var deserializer = new DeserializerBuilder().IgnoreUnmatchedProperties().Build(); this.Configurations = deserializer.Deserialize(file); @@ -37,9 +37,7 @@ namespace WinSW this.Configurations.LoadEnvironmentVariables(); } -#pragma warning disable CS8618 // Non-nullable field is uninitialized. Consider declaring as nullable. public ServiceDescriptorYaml(YamlConfiguration configs) -#pragma warning restore CS8618 // Non-nullable field is uninitialized. Consider declaring as nullable. { this.Configurations = configs; this.Configurations.LoadEnvironmentVariables(); diff --git a/src/WinSW.Core/Util/ConfigHelper.cs b/src/WinSW.Core/Util/ConfigHelper.cs index 8579a2b..2ff1978 100644 --- a/src/WinSW.Core/Util/ConfigHelper.cs +++ b/src/WinSW.Core/Util/ConfigHelper.cs @@ -19,7 +19,7 @@ namespace WinSW.Util return TimeSpan.FromMilliseconds(int.Parse(v)); } - private static readonly Dictionary Suffix = new Dictionary + private static readonly Dictionary Suffix = new() { { "ms", 1 }, { "sec", 1000L }, diff --git a/src/WinSW.Core/Util/ProcessHelper.cs b/src/WinSW.Core/Util/ProcessHelper.cs index 23171c6..a2385b2 100644 --- a/src/WinSW.Core/Util/ProcessHelper.cs +++ b/src/WinSW.Core/Util/ProcessHelper.cs @@ -29,11 +29,11 @@ namespace WinSW.Util try { string query = "SELECT * FROM Win32_Process WHERE ParentProcessID = " + pid; - using ManagementObjectSearcher searcher = new ManagementObjectSearcher(query); - using ManagementObjectCollection results = searcher.Get(); - foreach (ManagementBaseObject wmiObject in results) + using var searcher = new ManagementObjectSearcher(query); + using var results = searcher.Get(); + foreach (var wmiObject in results) { - var childProcessId = wmiObject["ProcessID"]; + object childProcessId = wmiObject["ProcessID"]; Logger.Info("Found child process: " + childProcessId + " Name: " + wmiObject["Name"]); childPids.Add(Convert.ToInt32(childProcessId)); } @@ -67,7 +67,7 @@ namespace WinSW.Util } // (bool sent, bool exited) - KeyValuePair result = SignalHelper.SendCtrlCToProcess(proc, stopTimeout); + var result = SignalHelper.SendCtrlCToProcess(proc, stopTimeout); bool exited = result.Value; if (!exited) { @@ -107,7 +107,7 @@ namespace WinSW.Util { if (!stopParentProcessFirst) { - foreach (var childPid in GetChildPids(pid)) + foreach (int childPid in GetChildPids(pid)) { StopProcessAndChildren(childPid, stopTimeout, stopParentProcessFirst); } @@ -117,7 +117,7 @@ namespace WinSW.Util if (stopParentProcessFirst) { - foreach (var childPid in GetChildPids(pid)) + foreach (int childPid in GetChildPids(pid)) { StopProcessAndChildren(childPid, stopTimeout, stopParentProcessFirst); } diff --git a/src/WinSW.Core/Util/XmlHelper.cs b/src/WinSW.Core/Util/XmlHelper.cs index e4878f4..f3e0e31 100644 --- a/src/WinSW.Core/Util/XmlHelper.cs +++ b/src/WinSW.Core/Util/XmlHelper.cs @@ -17,7 +17,7 @@ namespace WinSW.Util /// The required element is missing public static string? SingleElement(XmlNode node, string tagName, bool optional) { - XmlNode? n = node.SelectSingleNode(tagName); + var n = node.SelectSingleNode(tagName); if (n is null && !optional) { throw new InvalidDataException("<" + tagName + "> is missing in configuration XML"); @@ -36,7 +36,7 @@ namespace WinSW.Util /// The required element is missing public static XmlNode? SingleNode(XmlNode node, string tagName, bool optional) { - XmlNode? n = node.SelectSingleNode(tagName); + var n = node.SelectSingleNode(tagName); if (n is null && !optional) { throw new InvalidDataException("<" + tagName + "> is missing in configuration XML"); @@ -105,7 +105,7 @@ namespace WinSW.Util #if NET20 try { - var value = Enum.Parse(typeof(TAttributeType), substitutedValue, true); + object value = Enum.Parse(typeof(TAttributeType), substitutedValue, true); return (TAttributeType)value; } catch (ArgumentException ex) diff --git a/src/WinSW.Core/Wmi.cs b/src/WinSW.Core/Wmi.cs index fadec91..ed68947 100644 --- a/src/WinSW.Core/Wmi.cs +++ b/src/WinSW.Core/Wmi.cs @@ -87,7 +87,7 @@ namespace WMI public WmiRoot() { - ConnectionOptions options = new ConnectionOptions + var options = new ConnectionOptions { EnablePrivileges = true, Impersonation = ImpersonationLevel.Impersonate, @@ -118,7 +118,7 @@ namespace WMI protected ManagementBaseObject GetMethodParameters(ManagementObject wmiObject, string methodName, ParameterInfo[] methodParameters, object[] arguments) { - ManagementBaseObject wmiParameters = wmiObject.GetMethodParameters(methodName); + var wmiParameters = wmiObject.GetMethodParameters(methodName); for (int i = 0; i < arguments.Length; i++) { string capitalizedName = Capitalize(methodParameters[i].Name!); @@ -155,9 +155,9 @@ namespace WMI } string methodName = method.Name; - using ManagementBaseObject? wmiParameters = arguments.Length == 0 ? null : + using var wmiParameters = arguments.Length == 0 ? null : this.GetMethodParameters(this.wmiObject, methodName, method.GetParameters(), arguments); - using ManagementBaseObject result = this.wmiObject.InvokeMethod(methodName, wmiParameters, null); + using var result = this.wmiObject.InvokeMethod(methodName, wmiParameters, null); this.CheckError(result); return null; } @@ -176,12 +176,12 @@ namespace WMI public override object? Invoke(object proxy, MethodInfo method, object[] arguments) { - ParameterInfo[] methodParameters = method.GetParameters(); + var methodParameters = method.GetParameters(); if (method.Name == nameof(IWin32Services.Select)) { // select method to find instances - StringBuilder query = new StringBuilder("SELECT * FROM ").Append(this.className).Append(" WHERE "); + var query = new StringBuilder("SELECT * FROM ").Append(this.className).Append(" WHERE "); for (int i = 0; i < arguments.Length; i++) { if (i != 0) @@ -192,8 +192,8 @@ namespace WMI query.Append(' ').Append(Capitalize(methodParameters[i].Name!)).Append(" = '").Append(arguments[i]).Append('\''); } - using ManagementObjectSearcher searcher = new ManagementObjectSearcher(this.wmiClass.Scope, new ObjectQuery(query.ToString())); - using ManagementObjectCollection results = searcher.Get(); + using var searcher = new ManagementObjectSearcher(this.wmiClass.Scope, new ObjectQuery(query.ToString())); + using var results = searcher.Get(); // TODO: support collections foreach (ManagementObject wmiObject in results) @@ -205,9 +205,9 @@ namespace WMI } string methodName = method.Name; - using ManagementBaseObject? wmiParameters = arguments.Length == 0 ? null : + using var wmiParameters = arguments.Length == 0 ? null : this.GetMethodParameters(this.wmiClass, methodName, methodParameters, arguments); - using ManagementBaseObject result = this.wmiClass.InvokeMethod(methodName, wmiParameters, null); + using var result = this.wmiClass.InvokeMethod(methodName, wmiParameters, null); this.CheckError(result); return null; } @@ -219,7 +219,7 @@ namespace WMI public T GetCollection() where T : IWmiCollection { - WmiClassName className = (WmiClassName)typeof(T).GetCustomAttributes(typeof(WmiClassName), false)[0]; + var className = (WmiClassName)typeof(T).GetCustomAttributes(typeof(WmiClassName), false)[0]; return (T)ProxyFactory.Create(new ClassHandler(this.wmiScope, className.Name), typeof(T), true); } diff --git a/src/WinSW.Plugins/RunawayProcessKillerExtension.cs b/src/WinSW.Plugins/RunawayProcessKillerExtension.cs index 1ecab93..b20493c 100644 --- a/src/WinSW.Plugins/RunawayProcessKillerExtension.cs +++ b/src/WinSW.Plugins/RunawayProcessKillerExtension.cs @@ -96,7 +96,7 @@ namespace WinSW.Plugins { const int BaseBufferSize = 0x1000; string variableKey = '\0' + variable + '='; - string buffer = new string('\0', BaseBufferSize + variableKey.Length); + string buffer = new('\0', BaseBufferSize + variableKey.Length); fixed (char* bufferPtr = buffer) { long startAddress = address; @@ -171,7 +171,7 @@ namespace WinSW.Plugins _ = NtWow64QueryInformationProcess64( processHandle, PROCESSINFOCLASS.ProcessBasicInformation, - out PROCESS_BASIC_INFORMATION64 information, + out var information, sizeof(PROCESS_BASIC_INFORMATION64)); PEB64 peb; @@ -190,7 +190,7 @@ namespace WinSW.Plugins this.StopParentProcessFirst = bool.Parse(XmlHelper.SingleElement(node, "stopParentFirst", false)!); this.ServiceId = descriptor.Id; // TODO: Consider making it documented - var checkWinSWEnvironmentVariable = XmlHelper.SingleElement(node, "checkWinSWEnvironmentVariable", true); + string? checkWinSWEnvironmentVariable = XmlHelper.SingleElement(node, "checkWinSWEnvironmentVariable", true); this.CheckWinSWEnvironmentVariable = checkWinSWEnvironmentVariable is null ? true : bool.Parse(checkWinSWEnvironmentVariable); } @@ -282,7 +282,7 @@ namespace WinSW.Plugins } // Kill the runaway process - StringBuilder bldr = new StringBuilder("Stopping the runaway process (pid="); + var bldr = new StringBuilder("Stopping the runaway process (pid="); bldr.Append(pid); bldr.Append(") and its children. Environment was "); if (!this.CheckWinSWEnvironmentVariable) diff --git a/src/WinSW.Plugins/SharedDirectoryMapper.cs b/src/WinSW.Plugins/SharedDirectoryMapper.cs index e09c51f..c730072 100644 --- a/src/WinSW.Plugins/SharedDirectoryMapper.cs +++ b/src/WinSW.Plugins/SharedDirectoryMapper.cs @@ -10,8 +10,8 @@ namespace WinSW.Plugins { public class SharedDirectoryMapper : AbstractWinSWExtension { - private readonly SharedDirectoryMappingHelper _mapper = new SharedDirectoryMappingHelper(); - private readonly List _entries = new List(); + private readonly SharedDirectoryMappingHelper _mapper = new(); + private readonly List _entries = new(); public override string DisplayName => "Shared Directory Mapper"; @@ -23,13 +23,13 @@ namespace WinSW.Plugins public SharedDirectoryMapper(bool enableMapping, string directoryUNC, string driveLabel) { - SharedDirectoryMapperConfig config = new SharedDirectoryMapperConfig(enableMapping, driveLabel, directoryUNC); + var config = new SharedDirectoryMapperConfig(enableMapping, driveLabel, directoryUNC); this._entries.Add(config); } public override void Configure(IWinSWConfiguration descriptor, XmlNode node) { - XmlNodeList? mapNodes = XmlHelper.SingleNode(node, "mapping", false)!.SelectNodes("map"); + var mapNodes = XmlHelper.SingleNode(node, "mapping", false)!.SelectNodes("map"); if (mapNodes != null) { for (int i = 0; i < mapNodes.Count; i++) @@ -47,14 +47,14 @@ namespace WinSW.Plugins { var dict = config.GetSettings(); - var mappingNode = dict["mapping"]; + object mappingNode = dict["mapping"]; if (!(mappingNode is List mappings)) { throw new InvalidDataException("SharedDirectoryMapper mapping should be a list"); } - foreach (var map in mappings) + foreach (object map in mappings) { var mapConfig = SharedDirectoryMapperConfig.FromYaml(map); this._entries.Add(mapConfig); @@ -63,7 +63,7 @@ namespace WinSW.Plugins public override void OnWrapperStarted() { - foreach (SharedDirectoryMapperConfig config in this._entries) + foreach (var config in this._entries) { if (config.EnableMapping) { @@ -86,7 +86,7 @@ namespace WinSW.Plugins public override void BeforeWrapperStopped() { - foreach (SharedDirectoryMapperConfig config in this._entries) + foreach (var config in this._entries) { if (config.EnableMapping) { diff --git a/src/WinSW.Plugins/SharedDirectoryMapperHelper.cs b/src/WinSW.Plugins/SharedDirectoryMapperHelper.cs index 7ad7b88..65c0874 100644 --- a/src/WinSW.Plugins/SharedDirectoryMapperHelper.cs +++ b/src/WinSW.Plugins/SharedDirectoryMapperHelper.cs @@ -13,7 +13,7 @@ namespace WinSW.Plugins /// Operation failure private void InvokeCommand(string command, string args) { - Process p = new Process + var p = new Process { StartInfo = { diff --git a/src/WinSW.Tests/Configuration/ExamplesTest.cs b/src/WinSW.Tests/Configuration/ExamplesTest.cs index 4f8a12c..29ee0f2 100644 --- a/src/WinSW.Tests/Configuration/ExamplesTest.cs +++ b/src/WinSW.Tests/Configuration/ExamplesTest.cs @@ -17,7 +17,7 @@ namespace winswTests.Configuration [Test] public void AllOptionsConfigShouldDeclareDefaults() { - ServiceDescriptor desc = Load("allOptions"); + var desc = Load("allOptions"); Assert.That(desc.Id, Is.EqualTo("myapp")); Assert.That(desc.Caption, Is.EqualTo("MyApp Service (powered by WinSW)")); @@ -30,7 +30,7 @@ namespace winswTests.Configuration [Test] public void MinimalConfigShouldDeclareDefaults() { - ServiceDescriptor desc = Load("minimal"); + var desc = Load("minimal"); Assert.That(desc.Id, Is.EqualTo("myapp")); Assert.That(desc.Caption, Is.EqualTo("MyApp Service (powered by WinSW)")); @@ -57,7 +57,7 @@ namespace winswTests.Configuration string path = Path.Combine(directory, $@"examples\sample-{exampleName}.xml"); Assert.That(path, Does.Exist); - XmlDocument dom = new XmlDocument(); + var dom = new XmlDocument(); dom.Load(path); return new ServiceDescriptor(dom); } diff --git a/src/WinSW.Tests/DownloadConfigTests.cs b/src/WinSW.Tests/DownloadConfigTests.cs index b066a18..b3e45b4 100644 --- a/src/WinSW.Tests/DownloadConfigTests.cs +++ b/src/WinSW.Tests/DownloadConfigTests.cs @@ -15,7 +15,7 @@ namespace winswTests public void Roundtrip_Defaults() { // Roundtrip data - Download d = new Download(From, To); + var d = new Download(From, To); var sd = ConfigXmlBuilder.create() .WithDownload(d) .ToServiceDescriptor(true); @@ -33,7 +33,7 @@ namespace winswTests public void Roundtrip_BasicAuth() { // Roundtrip data - Download d = new Download(From, To, true, Download.AuthType.Basic, "aUser", "aPassword", true); + var d = new Download(From, To, true, Download.AuthType.Basic, "aUser", "aPassword", true); var sd = ConfigXmlBuilder.create() .WithDownload(d) .ToServiceDescriptor(true); @@ -51,7 +51,7 @@ namespace winswTests public void Roundtrip_SSPI() { // Roundtrip data - Download d = new Download(From, To, false, Download.AuthType.Sspi); + var d = new Download(From, To, false, Download.AuthType.Sspi); var sd = ConfigXmlBuilder.create() .WithDownload(d) .ToServiceDescriptor(true); @@ -98,7 +98,7 @@ namespace winswTests [TestCase(false)] public void Download_FailOnError(bool failOnError) { - Download d = new Download(From, To, failOnError); + var d = new Download(From, To, failOnError); var sd = ConfigXmlBuilder.create() .WithDownload(d) diff --git a/src/WinSW.Tests/DownloadTests.cs b/src/WinSW.Tests/DownloadTests.cs index 5c329bd..3c62c66 100644 --- a/src/WinSW.Tests/DownloadTests.cs +++ b/src/WinSW.Tests/DownloadTests.cs @@ -14,7 +14,7 @@ namespace winswTests [TestFixture] public class DownloadTests { - private readonly HttpListener globalListener = new HttpListener(); + private readonly HttpListener globalListener = new(); private readonly byte[] contents = { 0x57, 0x69, 0x6e, 0x53, 0x57 }; @@ -23,7 +23,7 @@ namespace winswTests [OneTimeSetUp] public void SetUp() { - TcpListener tcpListener = new TcpListener(IPAddress.Loopback, 0); + var tcpListener = new TcpListener(IPAddress.Loopback, 0); tcpListener.Start(); int port = ((IPEndPoint)tcpListener.LocalEndpoint).Port; string prefix = $"http://localhost:{port}/"; @@ -44,7 +44,7 @@ namespace winswTests private async Task TestClientServerAsync(Func client, Action server, AuthenticationSchemes authenticationSchemes = AuthenticationSchemes.Anonymous, [CallerMemberName] string path = null) { - HttpListener listener = new HttpListener(); + var listener = new HttpListener(); string prefix = $"{this.globalPrefix}{path}/"; listener.Prefixes.Add(prefix); listener.AuthenticationSchemes = authenticationSchemes; @@ -77,7 +77,7 @@ namespace winswTests async Task ListenAsync() { - HttpListenerContext context = await listener.GetContextAsync(); + var context = await listener.GetContextAsync(); try { server(context); @@ -141,7 +141,7 @@ namespace winswTests }, context => { - HttpListenerBasicIdentity identity = (HttpListenerBasicIdentity)context.User.Identity; + var identity = (HttpListenerBasicIdentity)context.User.Identity; if (identity.Name != username || identity.Password != password) { context.Response.StatusCode = (int)HttpStatusCode.Unauthorized; @@ -157,8 +157,8 @@ namespace winswTests [Test] public async Task TestHttp_IfModifiedSince_ModifiedAsync() { - DateTime lastModified = DateTime.Now.TrimToSeconds(); - DateTime prevModified = lastModified.AddDays(-1); + var lastModified = DateTime.Now.TrimToSeconds(); + var prevModified = lastModified.AddDays(-1); await this.TestClientServerAsync( async (source, dest) => @@ -185,7 +185,7 @@ namespace winswTests [Test] public async Task TestHttp_IfModifiedSince_NotModifiedAsync() { - DateTime lastModified = DateTime.Now.TrimToSeconds(); + var lastModified = DateTime.Now.TrimToSeconds(); await this.TestClientServerAsync( async (source, dest) => @@ -215,7 +215,7 @@ namespace winswTests await this.TestClientServerAsync( async (source, dest) => { - WebException exception = await AsyncAssert.ThrowsAsync( + var exception = await AsyncAssert.ThrowsAsync( async () => await new Download(source, dest).PerformAsync()); Assert.That(exception.Status, Is.EqualTo(WebExceptionStatus.ProtocolError)); diff --git a/src/WinSW.Tests/Extensions/RunawayProcessKillerTest.cs b/src/WinSW.Tests/Extensions/RunawayProcessKillerTest.cs index 134ec8f..51cb7d8 100644 --- a/src/WinSW.Tests/Extensions/RunawayProcessKillerTest.cs +++ b/src/WinSW.Tests/Extensions/RunawayProcessKillerTest.cs @@ -68,7 +68,7 @@ extensions: [Test] public void LoadExtensions() { - WinSWExtensionManager manager = new WinSWExtensionManager(this._testServiceDescriptor); + var manager = new WinSWExtensionManager(this._testServiceDescriptor); manager.LoadExtensions(); Assert.AreEqual(1, manager.Extensions.Count, "One extension should be loaded"); @@ -83,7 +83,7 @@ extensions: [Test] public void LoadExtensionsYaml() { - WinSWExtensionManager manager = new WinSWExtensionManager(this._testServiceDescriptorYaml); + var manager = new WinSWExtensionManager(this._testServiceDescriptorYaml); manager.LoadExtensions(); Assert.AreEqual(1, manager.Extensions.Count, "One extension should be loaded"); @@ -98,7 +98,7 @@ extensions: [Test] public void StartStopExtension() { - WinSWExtensionManager manager = new WinSWExtensionManager(this._testServiceDescriptor); + var manager = new WinSWExtensionManager(this._testServiceDescriptor); manager.LoadExtensions(); manager.FireOnWrapperStarted(); manager.FireBeforeWrapperStopped(); @@ -109,12 +109,12 @@ extensions: { Assert.Ignore(); - var winswId = "myAppWithRunaway"; - var extensionId = "runawayProcessKiller"; - var tmpDir = FilesystemTestHelper.CreateTmpDirectory(); + string winswId = "myAppWithRunaway"; + string extensionId = "runawayProcessKiller"; + string tmpDir = FilesystemTestHelper.CreateTmpDirectory(); // Spawn the test process - Process proc = new Process(); + var proc = new Process(); var ps = proc.StartInfo; ps.FileName = "cmd.exe"; ps.Arguments = "/c pause"; @@ -126,11 +126,11 @@ extensions: try { // Generate extension and ensure that the roundtrip is correct - var pidfile = Path.Combine(tmpDir, "process.pid"); + string pidfile = Path.Combine(tmpDir, "process.pid"); var sd = ConfigXmlBuilder.create(id: winswId) .WithRunawayProcessKiller(new RunawayProcessKillerExtension(pidfile), extensionId) .ToServiceDescriptor(); - WinSWExtensionManager manager = new WinSWExtensionManager(sd); + var manager = new WinSWExtensionManager(sd); manager.LoadExtensions(); var extension = manager.Extensions[extensionId] as RunawayProcessKillerExtension; Assert.IsNotNull(extension, "RunawayProcessKillerExtension should be loaded"); diff --git a/src/WinSW.Tests/Extensions/SharedDirectoryMapperTest.cs b/src/WinSW.Tests/Extensions/SharedDirectoryMapperTest.cs index 28b4f34..134898b 100644 --- a/src/WinSW.Tests/Extensions/SharedDirectoryMapperTest.cs +++ b/src/WinSW.Tests/Extensions/SharedDirectoryMapperTest.cs @@ -84,7 +84,7 @@ extensions: [Test] public void LoadExtensions() { - WinSWExtensionManager manager = new WinSWExtensionManager(this._testServiceDescriptor); + var manager = new WinSWExtensionManager(this._testServiceDescriptor); manager.LoadExtensions(); Assert.AreEqual(2, manager.Extensions.Count, "Two extensions should be loaded"); } @@ -92,7 +92,7 @@ extensions: [Test] public void LoadExtensionsYaml() { - WinSWExtensionManager manager = new WinSWExtensionManager(this._testServiceDescriptorYaml); + var manager = new WinSWExtensionManager(this._testServiceDescriptorYaml); manager.LoadExtensions(); Assert.AreEqual(2, manager.Extensions.Count, "Two extensions should be loaded"); } @@ -100,7 +100,7 @@ extensions: [Test] public void StartStopExtension() { - WinSWExtensionManager manager = new WinSWExtensionManager(this._testServiceDescriptor); + var manager = new WinSWExtensionManager(this._testServiceDescriptor); manager.LoadExtensions(); manager.FireOnWrapperStarted(); manager.FireBeforeWrapperStopped(); diff --git a/src/WinSW.Tests/MainTest.cs b/src/WinSW.Tests/MainTest.cs index c87b648..4425088 100644 --- a/src/WinSW.Tests/MainTest.cs +++ b/src/WinSW.Tests/MainTest.cs @@ -18,7 +18,7 @@ namespace winswTests { _ = CLITestHelper.CLITest(new[] { "install" }); - using ServiceController controller = new ServiceController(CLITestHelper.Id); + using var controller = new ServiceController(CLITestHelper.Id); Assert.That(controller.DisplayName, Is.EqualTo(CLITestHelper.Name)); Assert.That(controller.CanStop, Is.False); Assert.That(controller.CanShutdown, Is.False); @@ -61,7 +61,7 @@ namespace winswTests { const string commandName = "nonExistentCommand"; string expectedMessage = "Unknown command: " + commandName; - CLITestResult result = CLITestHelper.CLIErrorTest(new[] { commandName }); + var result = CLITestHelper.CLIErrorTest(new[] { commandName }); Assert.That(result.HasException, Is.True); Assert.That(result.Out, Does.Contain(expectedMessage)); diff --git a/src/WinSW.Tests/ServiceDescriptorYamlTest.cs b/src/WinSW.Tests/ServiceDescriptorYamlTest.cs index 96deddb..2b65dcc 100644 --- a/src/WinSW.Tests/ServiceDescriptorYamlTest.cs +++ b/src/WinSW.Tests/ServiceDescriptorYamlTest.cs @@ -463,7 +463,7 @@ delayedAutoStart: true"; [Test] public void Must_Specify_Values_Test() { - var yml = @" + string yml = @" name: This is a test executable: 'C:\Program Files\Java\jdk1.8.0_241\bin\java.exe' description: This is test winsw"; diff --git a/src/WinSW.Tests/Util/CLITestHelper.cs b/src/WinSW.Tests/Util/CLITestHelper.cs index 186e6f4..30bb722 100644 --- a/src/WinSW.Tests/Util/CLITestHelper.cs +++ b/src/WinSW.Tests/Util/CLITestHelper.cs @@ -36,11 +36,11 @@ $@" /// Command failure public static string CLITest(string[] arguments, ServiceDescriptor descriptor = null) { - TextWriter tmpOut = Console.Out; - TextWriter tmpErr = Console.Error; + var tmpOut = Console.Out; + var tmpErr = Console.Error; - using StringWriter swOut = new StringWriter(); - using StringWriter swErr = new StringWriter(); + using var swOut = new StringWriter(); + using var swErr = new StringWriter(); Console.SetOut(swOut); Console.SetError(swErr); @@ -68,11 +68,11 @@ $@" public static CLITestResult CLIErrorTest(string[] arguments, ServiceDescriptor descriptor = null) { Exception testEx = null; - TextWriter tmpOut = Console.Out; - TextWriter tmpErr = Console.Error; + var tmpOut = Console.Out; + var tmpErr = Console.Error; - using StringWriter swOut = new StringWriter(); - using StringWriter swErr = new StringWriter(); + using var swOut = new StringWriter(); + using var swErr = new StringWriter(); Console.SetOut(swOut); Console.SetError(swErr); diff --git a/src/WinSW.Tests/Util/ConfigXmlBuilder.cs b/src/WinSW.Tests/Util/ConfigXmlBuilder.cs index 3923a6b..6086c96 100644 --- a/src/WinSW.Tests/Util/ConfigXmlBuilder.cs +++ b/src/WinSW.Tests/Util/ConfigXmlBuilder.cs @@ -47,7 +47,7 @@ namespace winswTests.Util public string ToXMLString(bool dumpConfig = false) { - StringBuilder str = new StringBuilder(); + var str = new StringBuilder(); if (this.PrintXMLVersion) { // TODO: The encoding is generally wrong @@ -111,8 +111,8 @@ namespace winswTests.Util public ConfigXmlBuilder WithRunawayProcessKiller(RunawayProcessKillerExtension ext, string extensionId = "killRunawayProcess", bool enabled = true) { - var fullyQualifiedExtensionName = ExtensionTestBase.GetExtensionClassNameWithAssembly(typeof(RunawayProcessKillerExtension)); - StringBuilder str = new StringBuilder(); + string fullyQualifiedExtensionName = ExtensionTestBase.GetExtensionClassNameWithAssembly(typeof(RunawayProcessKillerExtension)); + var str = new StringBuilder(); str.AppendFormat(" \n", new object[] { enabled, fullyQualifiedExtensionName, extensionId }); str.AppendFormat(" {0}\n", ext.Pidfile); str.AppendFormat(" {0}\n", ext.StopTimeout.TotalMilliseconds); @@ -126,7 +126,7 @@ namespace winswTests.Util public ConfigXmlBuilder WithDownload(Download download) { - StringBuilder xml = new StringBuilder(); + var xml = new StringBuilder(); xml.Append($"Dictionary of the strings. public static Dictionary parseSetOutput(string filePath) { - Dictionary res = new Dictionary(); - var lines = File.ReadAllLines(filePath); - foreach (var line in lines) + var res = new Dictionary(); + string[] lines = File.ReadAllLines(filePath); + foreach (string line in lines) { - var parsed = line.Split("=".ToCharArray(), 2); + string[] parsed = line.Split("=".ToCharArray(), 2); if (parsed.Length == 2) { res.Add(parsed[0], parsed[1]); diff --git a/src/WinSW.Tests/Util/ProcessHelperTest.cs b/src/WinSW.Tests/Util/ProcessHelperTest.cs index 776423a..47222b7 100644 --- a/src/WinSW.Tests/Util/ProcessHelperTest.cs +++ b/src/WinSW.Tests/Util/ProcessHelperTest.cs @@ -20,17 +20,17 @@ namespace winswTests.Util Environment.SetEnvironmentVariable("TEST_KEY", "TEST_VALUE"); - var tmpDir = FilesystemTestHelper.CreateTmpDirectory(); + string tmpDir = FilesystemTestHelper.CreateTmpDirectory(); string envFile = Path.Combine(tmpDir, "env.properties"); string scriptFile = Path.Combine(tmpDir, "printenv.bat"); File.WriteAllText(scriptFile, "set > " + envFile); - Process proc = new Process(); + var proc = new Process(); var ps = proc.StartInfo; ps.FileName = scriptFile; ProcessHelper.StartProcessAndCallbackForExit(proc); - var exited = proc.WaitForExit(5000); + bool exited = proc.WaitForExit(5000); if (!exited) { Assert.Fail("Process " + proc + " didn't exit after 5 seconds"); @@ -50,17 +50,17 @@ namespace winswTests.Util [Test] public void ShouldNotHangWhenWritingLargeStringToStdOut() { - var tmpDir = FilesystemTestHelper.CreateTmpDirectory(); + string tmpDir = FilesystemTestHelper.CreateTmpDirectory(); string scriptFile = Path.Combine(tmpDir, "print_lots_to_stdout.bat"); - var lotsOfStdOut = string.Join(string.Empty, Enumerable.Range(1, 1000)); + string lotsOfStdOut = string.Join(string.Empty, Enumerable.Range(1, 1000)); File.WriteAllText(scriptFile, $"echo \"{lotsOfStdOut}\""); - Process proc = new Process(); + var proc = new Process(); var ps = proc.StartInfo; ps.FileName = scriptFile; ProcessHelper.StartProcessAndCallbackForExit(proc); - var exited = proc.WaitForExit(5000); + bool exited = proc.WaitForExit(5000); if (!exited) { Assert.Fail("Process " + proc + " didn't exit after 5 seconds"); diff --git a/src/WinSW.Tests/Util/ServiceDescriptorAssert.cs b/src/WinSW.Tests/Util/ServiceDescriptorAssert.cs index ed0ece3..14e9254 100644 --- a/src/WinSW.Tests/Util/ServiceDescriptorAssert.cs +++ b/src/WinSW.Tests/Util/ServiceDescriptorAssert.cs @@ -13,10 +13,10 @@ namespace winswTests.Util public static void AssertPropertyIsDefault(ServiceDescriptor desc, string property) { - PropertyInfo actualProperty = typeof(ServiceDescriptor).GetProperty(property); + var actualProperty = typeof(ServiceDescriptor).GetProperty(property); Assert.That(actualProperty, Is.Not.Null); - PropertyInfo defaultProperty = typeof(DefaultWinSWSettings).GetProperty(property); + var defaultProperty = typeof(DefaultWinSWSettings).GetProperty(property); Assert.That(defaultProperty, Is.Not.Null); Assert.That(actualProperty.GetValue(desc, null), Is.EqualTo(defaultProperty.GetValue(ServiceDescriptor.Defaults, null))); @@ -24,7 +24,7 @@ namespace winswTests.Util public static void AssertPropertyIsDefault(ServiceDescriptor desc, List properties) { - foreach (var prop in properties) + foreach (string prop in properties) { AssertPropertyIsDefault(desc, prop); } diff --git a/src/WinSW/Logging/WrapperServiceEventLogProvider.cs b/src/WinSW/Logging/WrapperServiceEventLogProvider.cs index 462b419..c53fb30 100644 --- a/src/WinSW/Logging/WrapperServiceEventLogProvider.cs +++ b/src/WinSW/Logging/WrapperServiceEventLogProvider.cs @@ -11,7 +11,7 @@ namespace WinSW.Logging public EventLog? Locate() { - WrapperService? service = this.Service; + var service = this.Service; if (service != null && !service.IsShuttingDown) { return service.EventLog; diff --git a/src/WinSW/Program.cs b/src/WinSW/Program.cs index 01553f5..380fae4 100644 --- a/src/WinSW/Program.cs +++ b/src/WinSW/Program.cs @@ -87,8 +87,8 @@ namespace WinSW } // Get service info for the future use - IWin32Services svcs = new WmiRoot().GetCollection(); - IWin32Service? svc = svcs.Select(descriptor.Id); + var svcs = new WmiRoot().GetCollection(); + var svc = svcs.Select(descriptor.Id); var args = new List(Array.AsReadOnly(argsArray)); if (args[0] == "/redirect") @@ -182,9 +182,7 @@ namespace WinSW default: Console.WriteLine("Unknown command: " + args[0]); PrintAvailableCommands(); -#pragma warning disable S112 // General exceptions should never be thrown throw new Exception("Unknown command: " + args[0]); -#pragma warning restore S112 // General exceptions should never be thrown } void Install() @@ -202,9 +200,7 @@ namespace WinSW { Console.WriteLine("Service with id '" + descriptor.Id + "' already exists"); Console.WriteLine("To install the service, delete the existing one or change service Id in the configuration file"); -#pragma warning disable S112 // General exceptions should never be thrown throw new Exception("Installation failure: Service with id '" + descriptor.Id + "' already exists"); -#pragma warning restore S112 // General exceptions should never be thrown } string? username = null; @@ -252,12 +248,12 @@ namespace WinSW password, descriptor.ServiceDependencies); - using ServiceManager scm = ServiceManager.Open(); - using Service sc = scm.OpenService(descriptor.Id); + using var scm = ServiceManager.Open(); + using var sc = scm.OpenService(descriptor.Id); sc.SetDescription(descriptor.Description); - SC_ACTION[] actions = descriptor.FailureActions; + var actions = descriptor.FailureActions; if (actions.Length > 0) { sc.SetFailureActions(descriptor.ResetFailureAfter, actions); @@ -460,18 +456,14 @@ namespace WinSW bool result = ProcessApis.CreateProcess(null, descriptor.ExecutablePath + " restart", IntPtr.Zero, IntPtr.Zero, false, ProcessApis.CREATE_NEW_PROCESS_GROUP, IntPtr.Zero, null, default, out _); if (!result) { -#pragma warning disable S112 // General exceptions should never be thrown throw new Exception("Failed to invoke restart: " + Marshal.GetLastWin32Error()); -#pragma warning restore S112 // General exceptions should never be thrown } } void Status() { Log.Debug("User requested the status of the process with id '" + descriptor.Id + "'"); -#pragma warning disable S3358 // Ternary operators should not be nested Console.WriteLine(svc is null ? "NonExistent" : svc.Started ? "Started" : "Stopped"); -#pragma warning restore S3358 // Ternary operators should not be nested } void Test() @@ -482,7 +474,7 @@ namespace WinSW return; } - WrapperService wsvc = new WrapperService(descriptor); + var wsvc = new WrapperService(descriptor); wsvc.RaiseOnStart(args.ToArray()); Thread.Sleep(1000); wsvc.RaiseOnStop(); @@ -496,7 +488,7 @@ namespace WinSW return; } - WrapperService wsvc = new WrapperService(descriptor); + var wsvc = new WrapperService(descriptor); wsvc.RaiseOnStart(args.ToArray()); Console.WriteLine("Press any key to stop the service..."); _ = Console.Read(); @@ -506,9 +498,9 @@ namespace WinSW // [DoesNotReturn] void Elevate() { - using Process current = Process.GetCurrentProcess(); + using var current = Process.GetCurrentProcess(); - ProcessStartInfo startInfo = new ProcessStartInfo + var startInfo = new ProcessStartInfo { UseShellExecute = true, Verb = "runas", @@ -525,7 +517,7 @@ namespace WinSW try { - using Process elevated = Process.Start(startInfo)!; + using var elevated = Process.Start(startInfo)!; elevated.WaitForExit(); Environment.Exit(elevated.ExitCode); @@ -544,18 +536,18 @@ namespace WinSW private static void InitLoggers(IWinSWConfiguration descriptor, bool enableConsoleLogging) { // TODO: Make logging levels configurable - Level fileLogLevel = Level.Debug; + var fileLogLevel = Level.Debug; // TODO: Debug should not be printed to console by default. Otherwise commands like 'status' will be pollutted // This is a workaround till there is a better command line parsing, which will allow determining - Level consoleLogLevel = Level.Info; - Level eventLogLevel = Level.Warn; + var consoleLogLevel = Level.Info; + var eventLogLevel = Level.Warn; // Legacy format from winsw-1.x: (DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " - " + message); - PatternLayout layout = new PatternLayout { ConversionPattern = "%d %-5p - %m%n" }; + var layout = new PatternLayout { ConversionPattern = "%d %-5p - %m%n" }; layout.ActivateOptions(); - List appenders = new List(); + var appenders = new List(); // .wrapper.log string wrapperLogPath = Path.Combine(descriptor.LogDirectory, descriptor.BaseName + ".wrapper.log"); @@ -604,8 +596,8 @@ namespace WinSW internal static unsafe bool IsProcessElevated() { - IntPtr process = ProcessApis.GetCurrentProcess(); - if (!ProcessApis.OpenProcessToken(process, TokenAccessLevels.Read, out IntPtr token)) + var process = ProcessApis.GetCurrentProcess(); + if (!ProcessApis.OpenProcessToken(process, TokenAccessLevels.Read, out var token)) { ThrowWin32Exception("Failed to open process token."); } @@ -615,7 +607,7 @@ namespace WinSW if (!SecurityApis.GetTokenInformation( token, SecurityApis.TOKEN_INFORMATION_CLASS.TokenElevation, - out SecurityApis.TOKEN_ELEVATION elevation, + out var elevation, sizeof(SecurityApis.TOKEN_ELEVATION), out _)) { @@ -631,17 +623,17 @@ namespace WinSW static void ThrowWin32Exception(string message) { - Win32Exception inner = new Win32Exception(); + var inner = new Win32Exception(); throw new Win32Exception(inner.NativeErrorCode, message + ' ' + inner.Message); } } private static string ReadPassword() { - StringBuilder buf = new StringBuilder(); + var buf = new StringBuilder(); while (true) { - ConsoleKeyInfo key = Console.ReadKey(true); + var key = Console.ReadKey(true); if (key.Key == ConsoleKey.Enter) { return buf.ToString(); @@ -661,8 +653,8 @@ namespace WinSW private static IWinSWConfiguration GetServiceDescriptor() { - var executablePath = new DefaultWinSWSettings().ExecutablePath; - var baseName = Path.GetFileNameWithoutExtension(executablePath); + string executablePath = new DefaultWinSWSettings().ExecutablePath; + string baseName = Path.GetFileNameWithoutExtension(executablePath); var d = new DirectoryInfo(Path.GetDirectoryName(executablePath)!); diff --git a/src/WinSW/WrapperService.cs b/src/WinSW/WrapperService.cs index 0740bfb..f5eb12f 100644 --- a/src/WinSW/WrapperService.cs +++ b/src/WinSW/WrapperService.cs @@ -22,7 +22,7 @@ namespace WinSW { private ServiceApis.SERVICE_STATUS wrapperServiceStatus; - private readonly Process process = new Process(); + private readonly Process process = new(); private readonly IWinSWConfiguration descriptor; @@ -36,7 +36,7 @@ namespace WinSW #endif "WinSW"); - internal static readonly WrapperServiceEventLogProvider eventLogProvider = new WrapperServiceEventLogProvider(); + internal static readonly WrapperServiceEventLogProvider eventLogProvider = new(); /// /// Indicates to the watch dog thread that we are going to terminate the process, @@ -79,7 +79,7 @@ namespace WinSW /// private void HandleFileCopies() { - var file = this.descriptor.BasePath + ".copies"; + string file = this.descriptor.BasePath + ".copies"; if (!File.Exists(file)) { return; // nothing to handle @@ -136,7 +136,7 @@ namespace WinSW Directory.CreateDirectory(logDirectory); } - LogHandler logAppender = this.descriptor.Log.CreateLogHandler(); + var logAppender = this.descriptor.Log.CreateLogHandler(); logAppender.EventLogger = this; return logAppender; @@ -198,11 +198,11 @@ namespace WinSW // handle downloads #if VNEXT - List downloads = this.descriptor.Downloads; - Task[] tasks = new Task[downloads.Count]; + var downloads = this.descriptor.Downloads; + var tasks = new Task[downloads.Count]; for (int i = 0; i < downloads.Count; i++) { - Download download = downloads[i]; + var download = downloads[i]; string downloadMessage = $"Downloading: {download.From} to {download.To}. failOnError={download.FailOnError.ToString()}"; this.LogEvent(downloadMessage); Log.Info(downloadMessage); @@ -215,14 +215,14 @@ namespace WinSW } catch (AggregateException e) { - List exceptions = new List(e.InnerExceptions.Count); + var exceptions = new List(e.InnerExceptions.Count); for (int i = 0; i < tasks.Length; i++) { if (tasks[i].IsFaulted) { - Download download = downloads[i]; + var download = downloads[i]; string errorMessage = $"Failed to download {download.From} to {download.To}"; - AggregateException exception = tasks[i].Exception!; + var exception = tasks[i].Exception!; this.LogEvent($"{errorMessage}. {exception.Message}"); Log.Error(errorMessage, exception); @@ -237,7 +237,7 @@ namespace WinSW throw new AggregateException(exceptions); } #else - foreach (Download download in this.descriptor.Downloads) + foreach (var download in this.descriptor.Downloads) { string downloadMessage = $"Downloading: {download.From} to {download.To}. failOnError={download.FailOnError.ToString()}"; this.LogEvent(downloadMessage); @@ -286,7 +286,7 @@ namespace WinSW this.ExtensionManager.LoadExtensions(); this.ExtensionManager.FireOnWrapperStarted(); - LogHandler executableLogHandler = this.CreateExecutableLogHandler(); + var executableLogHandler = this.CreateExecutableLogHandler(); this.StartProcess(this.process, startArguments, this.descriptor.Executable, executableLogHandler, true); this.ExtensionManager.FireOnProcessStarted(this.process); @@ -355,7 +355,7 @@ namespace WinSW stopArguments += " " + this.descriptor.Arguments; - Process stopProcess = new Process(); + var stopProcess = new Process(); string? executable = this.descriptor.StopExecutable; executable ??= this.descriptor.Executable; @@ -432,7 +432,7 @@ namespace WinSW private void SignalShutdownComplete() { - IntPtr handle = this.ServiceHandle; + var handle = this.ServiceHandle; this.wrapperServiceStatus.CheckPoint++; // WriteEvent("SignalShutdownComplete " + wrapperServiceStatus.checkPoint + ":" + wrapperServiceStatus.waitHint); this.wrapperServiceStatus.CurrentState = ServiceApis.ServiceState.STOPPED;