diff --git a/Main.cs b/Main.cs index 99bddda..2c56a7d 100644 --- a/Main.cs +++ b/Main.cs @@ -584,7 +584,7 @@ namespace winsw "\"" + d.ExecutablePath + "\"", ServiceType.OwnProcess, ErrorControl.UserNotified, - StartMode.Automatic, + d.StartMode, d.Interactive, username, password, diff --git a/README.markdown b/README.markdown index ee36f76..b52c4b2 100644 --- a/README.markdown +++ b/README.markdown @@ -171,6 +171,9 @@ Long human-readable description of the service. This gets displayed in Windows s ### executable This element specifies the executable to be launched. It can be either absolute path, or you can just specify the executable name and let it be searched from `PATH` (although note that the services often run in a different user account and therefore it might have different `PATH` than your shell does.) +### startmode - Optional Element +This element specifies the start mode of the Windows service. It can be one of the following values: Boot, System, Automatic, or Manual. See [MSDN](https://msdn.microsoft.com/en-us/library/aa384896%28v=vs.85%29.aspx) for details. The default is Automatic. + ### depend Specify IDs of other services that this service depends on. When service X depends on service Y, X can only run if Y is running. diff --git a/ServiceDescriptor.cs b/ServiceDescriptor.cs index 0949bd5..baa3e7b 100755 --- a/ServiceDescriptor.cs +++ b/ServiceDescriptor.cs @@ -5,6 +5,7 @@ using System.Diagnostics; using System.IO; using System.Reflection; using System.Xml; +using WMI; namespace winsw { @@ -393,6 +394,31 @@ namespace winsw } } + /// + /// Start mode of the Service + /// + public StartMode StartMode + { + get + { + var p = SingleElement("startmode", true); + if (p == null) return StartMode.Automatic; // default value + try + { + return (StartMode)Enum.Parse(typeof(StartMode), p, true); + } + catch + { + Console.WriteLine("Start mode in XML must be one of the following:"); + foreach (string sm in Enum.GetNames(typeof(StartMode))) + { + Console.WriteLine(sm); + } + throw; + } + } + } + /// /// True if the service should when finished on shutdown. /// This doesn't work on some OSes. See http://msdn.microsoft.com/en-us/library/ms679277%28VS.85%29.aspx diff --git a/Tests/winswTests/ServiceDescriptorTests.cs b/Tests/winswTests/ServiceDescriptorTests.cs index 9c0e5ab..7b7ebb1 100644 --- a/Tests/winswTests/ServiceDescriptorTests.cs +++ b/Tests/winswTests/ServiceDescriptorTests.cs @@ -5,6 +5,9 @@ using winsw; namespace winswTests { + using System; + using WMI; + [TestFixture] public class ServiceDescriptorTests { @@ -41,6 +44,66 @@ namespace winswTests _extendedServiceDescriptor = ServiceDescriptor.FromXML(seedXml); } + [Test] + public void DefaultStartMode() + { + Assert.That(_extendedServiceDescriptor.StartMode, Is.EqualTo(StartMode.Automatic)); + } + + [Test] + [ExpectedException(typeof(System.ArgumentException))] + public void IncorrectStartMode() + { + const string SeedXml = "" + + "service.exe" + + "Service" + + "The service." + + "node.exe" + + "My Arguments" + + "rotate" + + "rotate" + + "" + + "" + Domain + "" + + "" + Username + "" + + "" + Password + "" + + "" + AllowServiceAccountLogonRight + "" + + "" + + "" + + ExpectedWorkingDirectory + + "" + + @"C:\logs" + + ""; + + _extendedServiceDescriptor = ServiceDescriptor.FromXML(SeedXml); + Assert.That(_extendedServiceDescriptor.StartMode, Is.EqualTo(StartMode.Manual)); + } + + [Test] + public void ChangedStartMode() + { + const string SeedXml = "" + + "service.exe" + + "Service" + + "The service." + + "node.exe" + + "My Arguments" + + "manual" + + "rotate" + + "" + + "" + Domain + "" + + "" + Username + "" + + "" + Password + "" + + "" + AllowServiceAccountLogonRight + "" + + "" + + "" + + ExpectedWorkingDirectory + + "" + + @"C:\logs" + + ""; + + _extendedServiceDescriptor = ServiceDescriptor.FromXML(SeedXml); + Assert.That(_extendedServiceDescriptor.StartMode, Is.EqualTo(StartMode.Manual)); + } [Test] public void VerifyWorkingDirectory() {