2 // System.ServiceProcess.ServiceController
5 // Marek Safar (marek.safar@seznam.cz)
6 // Gert Driesen (drieseng@users.sourceforge.net)
8 // (C) 2005, Marek Safar
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 // TODO: check if there's more information to cache (eg. status)
34 using System.ComponentModel;
35 using System.Globalization;
37 using System.Runtime.InteropServices;
39 using System.ServiceProcess.Design;
40 using System.Threading;
42 namespace System.ServiceProcess
44 [Designer("System.ServiceProcess.Design.ServiceControllerDesigner, " + Consts.AssemblySystem_Design)]
45 [MonoTODO ("No unix implementation")]
47 [ServiceProcessDescription ("Provides the ability to connect to, query, and manipulate running or stopped Windows services.")]
49 public class ServiceController : Component
52 private string _serviceName = string.Empty;
53 private string _machineName;
54 private string _displayName = string.Empty;
55 private readonly ServiceControllerImpl _impl;
56 private ServiceController [] _dependentServices;
57 private ServiceController [] _servicesDependedOn;
59 public ServiceController ()
63 _impl = CreateServiceControllerImpl (this);
66 public ServiceController (string name) : this (name, ".")
70 public ServiceController (string name, string machineName)
72 if (name == null || name.Length == 0)
73 throw new ArgumentException (string.Format (CultureInfo.CurrentCulture,
74 "Invalid value {0} for parameter name.", name));
76 ValidateMachineName (machineName);
78 _machineName = machineName;
80 _impl = CreateServiceControllerImpl (this);
83 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
84 [ServiceProcessDescription ("Whether this service recognizes the Pause and Continue commands.")]
85 public bool CanPauseAndContinue {
88 ValidateServiceName (ServiceName);
90 return _impl.CanPauseAndContinue;
94 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
95 [ServiceProcessDescription ("Whether this service can respond to a system shutdown.")]
96 public bool CanShutdown {
100 ValidateServiceName (ServiceName);
102 return _impl.CanShutdown;
106 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
107 [ServiceProcessDescription ("Whether this service can be stopped.")]
108 public bool CanStop {
112 ValidateServiceName (ServiceName);
114 return _impl.CanStop;
118 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
119 [ServiceProcessDescription ("The services that depend on this service in order to run.")]
120 public ServiceController [] DependentServices {
124 ValidateServiceName (ServiceName);
126 if (_dependentServices == null)
127 _dependentServices = _impl.DependentServices;
128 return _dependentServices;
132 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
134 [ServiceProcessDescription ("The descriptive name of the service.")]
135 public string DisplayName {
137 if (_displayName.Length == 0 && (_serviceName.Length > 0 || _name.Length > 0))
138 _displayName = _impl.DisplayName;
143 throw new ArgumentNullException ("value");
145 if (_displayName == value)
148 _displayName = value;
150 // if display name is modified, then we also need to force a
151 // new lookup of the corresponding service name
152 _serviceName = string.Empty;
154 // you'd expect the DependentServices and ServiceDependedOn cache
155 // to be cleared too, but the MS implementation doesn't do this
157 // categorized as by design:
158 // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=201762
160 // release any handles and clear cache
167 [RecommendedAsConfigurable (true)]
168 [ServiceProcessDescription ("The name of the machine on which this service resides.")]
169 public string MachineName {
174 ValidateMachineName (value);
176 if (_machineName == value)
179 _machineName = value;
181 // you'd expect the DependentServices and ServiceDependedOn cache
182 // to be cleared too, but the MS implementation doesn't do this
184 // categorized as by design:
185 // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=201762
187 // release any handles and clear cache
194 [RecommendedAsConfigurable (true)]
195 [ServiceProcessDescription ("The short name of the service.")]
196 [TypeConverter (typeof (ServiceNameConverter))]
197 public string ServiceName {
199 if (_serviceName.Length == 0 && (_displayName.Length > 0 || _name.Length > 0))
200 _serviceName = _impl.ServiceName;
205 throw new ArgumentNullException ("value");
207 if (_serviceName == value)
211 ValidateServiceName (value);
214 _serviceName = value;
216 // if service name is modified, then we also need to force a
217 // new lookup of the corresponding display name
218 _displayName = string.Empty;
220 // you'd expect the DependentServices and ServiceDependedOn cache
221 // to be cleared too, but the MS implementation doesn't do this
223 // categorized as by design:
224 // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=201762
226 // release any handles and clear cache
231 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
232 [ServiceProcessDescription ("Services that must be started in order for this one to start.")]
233 public ServiceController [] ServicesDependedOn {
237 ValidateServiceName (ServiceName);
239 if (_servicesDependedOn == null)
240 _servicesDependedOn = _impl.ServicesDependedOn;
241 return _servicesDependedOn;
248 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
249 public SafeHandle ServiceHandle
252 throw new NotImplementedException ();
257 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
258 [ServiceProcessDescription ("The type of this service.")]
259 public ServiceType ServiceType {
263 ValidateServiceName (ServiceName);
265 return _impl.ServiceType;
269 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
270 [ServiceProcessDescription ("The status of the service, e.g., Running or Stopped.")]
271 public ServiceControllerStatus Status {
275 ValidateServiceName (ServiceName);
286 public void Continue ()
289 ValidateServiceName (ServiceName);
294 protected override void Dispose (bool disposing)
296 _impl.Dispose (disposing);
297 base.Dispose (disposing);
300 public void ExecuteCommand (int command)
303 ValidateServiceName (ServiceName);
305 _impl.ExecuteCommand (command);
308 public static ServiceController[] GetDevices ()
310 return GetDevices (".");
313 public static ServiceController[] GetDevices (string machineName)
315 ValidateMachineName (machineName);
317 using (ServiceController sc = new ServiceController ("dummy", machineName)) {
318 ServiceControllerImpl impl = CreateServiceControllerImpl (sc);
319 return impl.GetDevices ();
323 public static ServiceController[] GetServices ()
325 return GetServices (".");
328 public static ServiceController[] GetServices (string machineName)
330 ValidateMachineName (machineName);
332 using (ServiceController sc = new ServiceController ("dummy", machineName)) {
333 ServiceControllerImpl impl = CreateServiceControllerImpl (sc);
334 return impl.GetServices ();
341 ValidateServiceName (ServiceName);
346 public void Refresh ()
348 // MSDN: this method also sets the ServicesDependedOn and
349 // DependentServices properties to a null reference
351 // I assume they wanted to say that the cache for these properties
352 // is cleared. Verified by unit tests.
353 _dependentServices = null;
354 _servicesDependedOn = null;
360 Start (new string [0]);
363 public void Start (string [] args)
366 ValidateServiceName (ServiceName);
374 ValidateServiceName (ServiceName);
379 public void WaitForStatus (ServiceControllerStatus desiredStatus)
381 WaitForStatus (desiredStatus, TimeSpan.MaxValue);
384 public void WaitForStatus (ServiceControllerStatus desiredStatus, TimeSpan timeout)
387 ValidateServiceName (ServiceName);
390 DateTime start = DateTime.Now;
391 while (Status != desiredStatus) {
392 if (timeout < (DateTime.Now - start))
393 throw new TimeoutException ("Time out has expired and the"
394 + " operation has not been completed.");
396 // force refresh of status
401 internal string Name {
410 internal string InternalDisplayName {
415 _displayName = value;
419 internal string InternalServiceName {
424 _serviceName = value;
429 private static void ValidateServiceName (string serviceName)
431 if (serviceName.Length == 0 || serviceName.Length > 80)
432 throw new ArgumentException (string.Format (CultureInfo.CurrentCulture,
433 "Service name {0} contains invalid characters, is empty"
434 + " or is too long (max length = 80).", serviceName));
438 private static void ValidateMachineName (string machineName)
440 if (machineName == null || machineName.Length == 0)
441 throw new ArgumentException (string.Format (CultureInfo.CurrentCulture,
442 "MachineName value {0} is invalid.", machineName));
445 private static ServiceControllerImpl CreateServiceControllerImpl (ServiceController serviceController)
447 int p = (int) Environment.OSVersion.Platform;
449 if (p == 4 || p == 128 || p == 6){
450 return new UnixServiceController (serviceController);
452 return new Win32ServiceController (serviceController);