Scalable Development, Inc.
Building systems today that perform tomorrow.
Designing & Building
Windows Services
with VB.NET
 Wallace
B. McClure
 Scalable
Development, Inc.
.NET Experiences
2000 Build (July 2000).
 Visual Studio 1.0 Beta 1 (November 2000).
 Book began (January 2001).
 Visual Studio 1.0 Beta 2 (June 2001).
 First Production ASP.NET App (July 2001).
 Production Windows Service (November 2001).
Runs today.
 4 Production Applications by shipment.
 Multiple running applications.
.NET Resources
 AspAdvice –
 Windows Forms –
 Architecture –
 .NET News –
What are Windows Services?
 Applications
(Database, Web Server, …).
 Good for long running / complicated operations.
 Run all the time.
 No User Interface.
 Run within their own security context.
 Limited access to local resources.
 Limited access to remote resources.
 Debugging.
 Non-interactive.
Design Guidelines
 Consistency.
 No
popup messages.
 Information/Errors
need to be written to
 Be
careful blocking.
Types of .NET Applications
 Web
 WinForms.
 Components.
 Windows
 Others.
.NET Support for Services
 System.ServiceProcess
 Inherit
from the ServiceBase Class.
 Installation.
 ServiceController
Class Allows communication from
authorized user (WinForms, ASP.NET, or other) to a
Service (thru SCM).
Languages Support
 C++
(Managed &
 Visual
 C#.
 Other
.NET Languages.
Parts of a .NET Windows Service
 Service
Control Manager (SCM).
 System.ServiceProcess.ServiceBase
 Events.
 Installation.
Process Installation.
Service Installation.
Events in ServiceBase
 OnStart().
 OnStop().
 OnPause().
 OnContinue().
 OnShutdown().
 OnPowerEvent().
 OnCustomCommand().
OnStart() Event
 Called
when the Service is issued
the start command.
 VB
Protected Overridable Sub OnStart
( _ByVal args() as String )
 Hard
to Debug By Default.
Debugging the OnStart() Event
 Create
a dummy service that is a part of
your process.
 Start the dummy service to start the
 Attach to running process.
 Place breakpoint.
 Start “real” service.
OnStop() Event
 Called
when the Service is issued the
stop command.
 Protected
Overridable Sub OnStop().
OnPause() Event
 Called
when the Service is issued the
pause command.
 Protected
Overrideable Sub
OnContinue() Event
 Called
when the Service is issued
the continue command.
 Protected
Overrideable Sub
OnShutdown() Event
 Called
when the System sends the
shutdown command to all Applications
specifying that a system shutdown is
 Similar
to the OnStop() event.
 Protected
Overrideable Sub OnShutdown().
OnPowerEvent() Event
Called when the computer’s power status has changed.
Typically, this applies to a laptop computer when it goes into a
suspended state.
Not the same as a system shutdown.
Protected Overrideable Function OnPowerEvent( ByVal
powerStatus as PowerBroadcastStatus ) as Boolean
Boolean return value is a response to a QuerySuspend
True = Application is in a state where a suspend is ok. False
= suspend is not ok.
PowerBroadcastStatus is an enumertion with 9 values.
OnCustomCommand() Event
 Executed
when a custom command is
passed from the SCM to the service.
 Protected Overridable Sub
OnCustomCommand( ByVal
command as Integer )
 Command values between 128 & 255.
Security Context
Service runs within a defined security context
 What you do not necessarily
have access to:
 Desktop.
 Remote Resources.
 Mapped Drives.
 What you do have access to:
 Local FileSystem.
 Network Protocols (TCP/IP, …).
 Database (ODBC, OleDb, MP).
Custom Commands /
 Use
the ServiceController class.
 ExecuteCommand( ByVal command as
Integer ) method.
 Start, Stop, Pause, Continue.
 MachineName, ServiceName, Status
 Each
executable must have a process
 Each
service within a process must have
a service installer.
 Command
line utility (installutil.exe)
 Excellent
for read only
Great, Now What can
You do with a Service?
 Listen
for events to occur.
 Network requests.
 Timer countdown.
 Messages arriving in a message
 File system changes.
 Other.
 Timer.
 When
the Timer counts down to zero,
an event fires and our application
performs an operation.
 No
continual polling occurs, no
blocking, and no extra processing
occurs on the system.
What’s Not to Like?
 Requires
the .NET Framework.
 If
a framework exception occurs when the
framework stops, the Windows Service
stops…….and there is nothing within the
framework to restart that Windows Service.
 Problem
is rare, but possible. (ODP.NET users, problem has been
Monitoring a Windows Service
 Need
something that won’t stop.
 Can do it with .NET and a Console application.
 Drop back to COM/API.
 Use WMI & “Scheduled Tasks.”
 Use ServiceController class.
 Run every few days/hours/minutes to monitor the
status of your Service.
Things to look at / Last Thoughts
 Event
Processing vs. Blocking.
 EventLog.
 Multiple Threads of Execution.
 Weak References.
 Performance Monitor
 Nothing wrong with Interop.
Scalable Development, Inc.
Building systems today that perform tomorrow.
 Scalable
Development, Inc.
 Consulting & Development Services.
 865-693-3004.
 [email protected]

Overview of ADO.NET Whidbey