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;
36 using System.Runtime.InteropServices;
37 using System.ServiceProcess.Design;
38 using System.Threading;
40 namespace System.ServiceProcess
42 [Designer("System.ServiceProcess.Design.ServiceControllerDesigner, " + Consts.AssemblySystem_Design)]
43 [MonoTODO ("No unix implementation")]
44 [ServiceProcessDescription ("Provides the ability to connect to, query, and manipulate running or stopped Windows services.")]
45 public class ServiceController : Component
48 private string _serviceName = string.Empty;
49 private string _machineName;
50 private string _displayName = string.Empty;
51 private readonly ServiceControllerImpl _impl;
52 private ServiceController [] _dependentServices;
53 private ServiceController [] _servicesDependedOn;
55 public ServiceController ()
59 _impl = CreateServiceControllerImpl (this);
62 public ServiceController (string name) : this (name, ".")
66 public ServiceController (string name, string machineName)
68 if (name == null || name.Length == 0)
69 throw new ArgumentException (string.Format (CultureInfo.CurrentCulture,
70 "Invalid value {0} for parameter name.", name));
72 ValidateMachineName (machineName);
74 _machineName = machineName;
76 _impl = CreateServiceControllerImpl (this);
79 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
80 [ServiceProcessDescription ("Whether this service recognizes the Pause and Continue commands.")]
81 public bool CanPauseAndContinue {
83 ValidateServiceName (ServiceName);
84 return _impl.CanPauseAndContinue;
88 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
89 [ServiceProcessDescription ("Whether this service can respond to a system shutdown.")]
90 public bool CanShutdown {
93 ValidateServiceName (ServiceName);
94 return _impl.CanShutdown;
98 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
99 [ServiceProcessDescription ("Whether this service can be stopped.")]
100 public bool CanStop {
103 ValidateServiceName (ServiceName);
104 return _impl.CanStop;
108 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
109 [ServiceProcessDescription ("The services that depend on this service in order to run.")]
110 public ServiceController [] DependentServices {
113 ValidateServiceName (ServiceName);
114 if (_dependentServices == null)
115 _dependentServices = _impl.DependentServices;
116 return _dependentServices;
120 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
122 [ServiceProcessDescription ("The descriptive name of the service.")]
123 public string DisplayName {
125 if (_displayName.Length == 0 && (_serviceName.Length > 0 || _name.Length > 0))
126 _displayName = _impl.DisplayName;
131 throw new ArgumentNullException ("value");
133 if (_displayName == value)
136 _displayName = value;
138 // if display name is modified, then we also need to force a
139 // new lookup of the corresponding service name
140 _serviceName = string.Empty;
142 // you'd expect the DependentServices and ServiceDependedOn cache
143 // to be cleared too, but the MS implementation doesn't do this
145 // categorized as by design:
146 // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=201762
148 // release any handles and clear cache
155 [RecommendedAsConfigurable (true)]
156 [ServiceProcessDescription ("The name of the machine on which this service resides.")]
157 public string MachineName {
162 ValidateMachineName (value);
164 if (_machineName == value)
167 _machineName = value;
169 // you'd expect the DependentServices and ServiceDependedOn cache
170 // to be cleared too, but the MS implementation doesn't do this
172 // categorized as by design:
173 // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=201762
175 // release any handles and clear cache
182 [RecommendedAsConfigurable (true)]
183 [ServiceProcessDescription ("The short name of the service.")]
184 [TypeConverter (typeof (ServiceNameConverter))]
185 public string ServiceName {
187 if (_serviceName.Length == 0 && (_displayName.Length > 0 || _name.Length > 0))
188 _serviceName = _impl.ServiceName;
193 throw new ArgumentNullException ("value");
195 if (_serviceName == value)
198 ValidateServiceName (value);
200 _serviceName = value;
202 // if service name is modified, then we also need to force a
203 // new lookup of the corresponding display name
204 _displayName = string.Empty;
206 // you'd expect the DependentServices and ServiceDependedOn cache
207 // to be cleared too, but the MS implementation doesn't do this
209 // categorized as by design:
210 // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=201762
212 // release any handles and clear cache
217 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
218 [ServiceProcessDescription ("Services that must be started in order for this one to start.")]
219 public ServiceController [] ServicesDependedOn {
222 ValidateServiceName (ServiceName);
223 if (_servicesDependedOn == null)
224 _servicesDependedOn = _impl.ServicesDependedOn;
225 return _servicesDependedOn;
231 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
232 public SafeHandle ServiceHandle
235 throw new NotImplementedException ();
239 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
240 [ServiceProcessDescription ("The type of this service.")]
241 public ServiceType ServiceType {
244 ValidateServiceName (ServiceName);
245 return _impl.ServiceType;
249 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
250 [ServiceProcessDescription ("The status of the service, e.g., Running or Stopped.")]
251 public ServiceControllerStatus Status {
254 ValidateServiceName (ServiceName);
264 public void Continue ()
266 ValidateServiceName (ServiceName);
270 protected override void Dispose (bool disposing)
272 _impl.Dispose (disposing);
273 base.Dispose (disposing);
276 public void ExecuteCommand (int command)
278 ValidateServiceName (ServiceName);
279 _impl.ExecuteCommand (command);
282 public static ServiceController[] GetDevices ()
284 return GetDevices (".");
287 public static ServiceController[] GetDevices (string machineName)
289 ValidateMachineName (machineName);
291 using (ServiceController sc = new ServiceController ("dummy", machineName)) {
292 ServiceControllerImpl impl = CreateServiceControllerImpl (sc);
293 return impl.GetDevices ();
297 public static ServiceController[] GetServices ()
299 return GetServices (".");
302 public static ServiceController[] GetServices (string machineName)
304 ValidateMachineName (machineName);
306 using (ServiceController sc = new ServiceController ("dummy", machineName)) {
307 ServiceControllerImpl impl = CreateServiceControllerImpl (sc);
308 return impl.GetServices ();
314 ValidateServiceName (ServiceName);
318 public void Refresh ()
320 // MSDN: this method also sets the ServicesDependedOn and
321 // DependentServices properties to a null reference
323 // I assume they wanted to say that the cache for these properties
324 // is cleared. Verified by unit tests.
325 _dependentServices = null;
326 _servicesDependedOn = null;
332 Start (new string [0]);
335 public void Start (string [] args)
337 ValidateServiceName (ServiceName);
343 ValidateServiceName (ServiceName);
347 public void WaitForStatus (ServiceControllerStatus desiredStatus)
349 WaitForStatus (desiredStatus, TimeSpan.MaxValue);
352 public void WaitForStatus (ServiceControllerStatus desiredStatus, TimeSpan timeout)
354 ValidateServiceName (ServiceName);
356 DateTime start = DateTime.Now;
357 while (Status != desiredStatus) {
358 if (timeout < (DateTime.Now - start))
359 throw new TimeoutException ("Time out has expired and the"
360 + " operation has not been completed.");
362 // force refresh of status
367 internal string Name {
376 internal string InternalDisplayName {
381 _displayName = value;
385 internal string InternalServiceName {
390 _serviceName = value;
394 private static void ValidateServiceName (string serviceName)
396 if (serviceName.Length == 0 || serviceName.Length > 80)
397 throw new ArgumentException (string.Format (CultureInfo.CurrentCulture,
398 "Service name {0} contains invalid characters, is empty"
399 + " or is too long (max length = 80).", serviceName));
402 private static void ValidateMachineName (string machineName)
404 if (machineName == null || machineName.Length == 0)
405 throw new ArgumentException (string.Format (CultureInfo.CurrentCulture,
406 "MachineName value {0} is invalid.", machineName));
409 private static ServiceControllerImpl CreateServiceControllerImpl (ServiceController serviceController)
411 int p = (int) Environment.OSVersion.Platform;
413 if (p == 4 || p == 128 || p == 6){
414 return new UnixServiceController (serviceController);
416 return new Win32ServiceController (serviceController);