Merge pull request #637 from LogosBible/enetdown
[mono.git] / mcs / class / System.ServiceProcess / System.ServiceProcess / ServiceBase.cs
1 //
2 // System.ServiceProcess.ServiceBase.cs
3 //
4 // Authors:
5 //      Cesar Octavio Lopez Nataren (cesar@ciencias.unam.mx)
6 //      Duncan Mak (duncan@ximian.com)
7 //      Joerg Rosenkranz (joergr@voelcker.com)
8 //
9 // (C) 2003, Ximian Inc and Cesar Octavio Lopez Nataren.
10 // (C) 2005, Voelcker Informatik AG
11 //
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32
33 using System;
34 using System.ComponentModel;
35 using System.Globalization;
36 using System.Diagnostics;
37 using System.Runtime.InteropServices;
38
39 namespace System.ServiceProcess
40 {
41 #if ONLY_1_1
42         [Designer ("Microsoft.VisualStudio.Install.UserNTServiceDesigner, " + Consts.AssemblyMicrosoft_VisualStudio, "System.ComponentModel.Design.IRootDesigner")]
43 #endif
44         [InstallerType (typeof (ServiceProcessInstaller))]
45         public class ServiceBase : Component
46         {
47                 internal delegate void RunServiceCallback (ServiceBase [] services);
48
49                 // This member is used for interoperation with mono-service
50                 internal static RunServiceCallback RunService;
51
52                 public const int MaxNameLength = 80;
53
54                 bool hasStarted;
55                 bool auto_log = true;
56                 bool can_handle_power_event;
57                 bool can_pause_and_continue;
58                 bool can_shutdown;
59                 bool can_stop = true;
60                 EventLog event_log;
61                 string service_name;
62 #if NET_2_0
63                 bool can_handle_session_change_event;
64 #endif
65
66                 public ServiceBase ()
67                 {
68                 }
69
70                 [DefaultValue (true)]
71                 [ServiceProcessDescription ("Whether the service should automatically write to the event log on common events such as Install and Start.")]
72                 public bool AutoLog {
73                         get { return auto_log; }
74                         set { auto_log = value; }
75                 }
76
77                 [DefaultValue (false)]
78                 public bool CanHandlePowerEvent {
79                         get { return can_handle_power_event; }
80                         set {
81                                 if (hasStarted)
82                                         throw new InvalidOperationException (
83                                                         Locale.GetText ("Cannot modify this property " +
84                                                                                         "after the service has started."));
85
86                                 can_handle_power_event = value;
87                         }
88                 }
89
90 #if NET_2_0
91                 [DefaultValue (false)]
92                 [MonoTODO]
93                 [ComVisible (false)]
94                 public bool CanHandleSessionChangeEvent {
95                         get { return can_handle_session_change_event; }
96                         set {
97                                 if (hasStarted)
98                                         throw new InvalidOperationException (
99                                                         Locale.GetText ("Cannot modify this property " +
100                                                                                         "after the service has started."));
101
102                                 can_handle_session_change_event = value;
103                         }
104                 }
105 #endif
106
107                 [DefaultValue (false)]
108                 public bool CanPauseAndContinue {
109                         get { return can_pause_and_continue; }
110                         set {
111                                 if (hasStarted)
112                                         throw new InvalidOperationException (
113                                                         Locale.GetText ("Cannot modify this property " +
114                                                                                         "after the service has started."));
115
116                                 can_pause_and_continue = value;
117                         }
118                 }
119
120                 [DefaultValue (false)]
121                 public bool CanShutdown {
122                         get { return can_shutdown; }
123                         set {
124                                 if (hasStarted)
125                                         throw new InvalidOperationException (
126                                                         Locale.GetText ("Cannot modify this property " +
127                                                                                         "after the service has started."));
128
129                                 can_shutdown = value;
130                         }
131                 }
132
133                 [DefaultValue (true)]
134                 public bool CanStop {
135                         get { return can_stop; }
136                         set {
137                                 if (hasStarted)
138                                         throw new InvalidOperationException (
139                                                         Locale.GetText ("Cannot modify this property " +
140                                                                                         "after the service has started."));
141
142                                 can_stop = value;
143                         }
144                 }
145
146                 [Browsable (false)]
147                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
148                 public virtual EventLog EventLog {
149                         get {
150                                 if (event_log == null)
151                                         event_log = new EventLog ("Application", ".", service_name);
152                                 return event_log;
153                         }
154                 }
155
156 #if NET_2_0
157                 [ComVisible (false)]
158                 public int ExitCode { get; set; }
159
160                 [MonoTODO]
161                 [EditorBrowsable (EditorBrowsableState.Advanced)]
162                 protected IntPtr ServiceHandle {
163                         get { throw new NotImplementedException (); }
164                 }
165 #endif
166
167                 [ServiceProcessDescription ("The name by which the service is identified to the system.")]
168                 [TypeConverter ("System.Diagnostics.Design.StringValueConverter, " + Consts.AssemblySystem_Design)]
169                 public string ServiceName {
170                         get { return service_name; }
171                         set {
172                                 if (hasStarted)
173                                         throw new InvalidOperationException (
174                                                         Locale.GetText ("Cannot modify this property " +
175                                                                                         "after the service has started."));
176
177                                 service_name = value;
178                         }
179                 }
180
181                 protected override void Dispose (bool disposing)
182                 {
183                 }
184
185                 protected virtual void OnStart (string [] args)
186                 {
187                 }
188
189                 protected virtual void OnStop ()
190                 {
191                 }
192
193                 protected virtual void OnContinue ()
194                 {
195                 }
196
197                 protected virtual void OnCustomCommand (int command)
198                 {
199                 }
200
201                 protected virtual void OnPause ()
202                 {
203                 }
204
205                 protected virtual bool OnPowerEvent (PowerBroadcastStatus powerStatus)
206                 {
207                         return true;
208                 }
209
210                 protected virtual void OnShutdown ()
211                 {
212                 }
213
214 #if NET_2_0
215                 protected virtual void OnSessionChange (SessionChangeDescription changeDescription)
216                 {
217                 }
218
219                 [ComVisible (false)]
220                 [MonoTODO]
221                 public void RequestAdditionalTime (int milliseconds)
222                 {
223                         throw new NotImplementedException ();
224                 }
225
226                 [ComVisible (false)]
227                 [EditorBrowsable (EditorBrowsableState.Never)]
228                 [MonoTODO]
229                 public void ServiceMainCallback (int argCount, IntPtr argPointer)
230                 {
231                         throw new NotImplementedException ();
232                 }
233
234                 public void Stop ()
235                 {
236                         OnStop ();
237                 }
238 #endif
239
240                 public static void Run (ServiceBase service)
241                 {
242                         Run (new ServiceBase [] { service });
243                 }
244
245                 public static void Run (ServiceBase [] services)
246                 {
247                         if (RunService != null)
248                                 RunService (services);
249                 }
250         }
251 }