building windows services in vb
TRANSCRIPT
![Page 1: Building Windows Services in VB](https://reader036.vdocuments.net/reader036/viewer/2022062512/5528f1d1550346662e8b458e/html5/thumbnails/1.jpg)
Wallace B. McClure
Scalable Development, Inc.
Scalable Development, Inc.Building systems today that perform tomorrow.
Designing & BuildingWindows Services
with VB.NET
![Page 2: Building Windows Services in VB](https://reader036.vdocuments.net/reader036/viewer/2022062512/5528f1d1550346662e8b458e/html5/thumbnails/2.jpg)
.NET Experiences
PDC 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.
![Page 3: Building Windows Services in VB](https://reader036.vdocuments.net/reader036/viewer/2022062512/5528f1d1550346662e8b458e/html5/thumbnails/3.jpg)
.NET Resources
ASP.NET – www.asp.netAspAdvice – www.aspadvice.comWindows Forms – www.windowsforms.netArchitecture –
msdn.microsoft.com/architecture.NET News – www.dotnetwire.com
![Page 4: Building Windows Services in VB](https://reader036.vdocuments.net/reader036/viewer/2022062512/5528f1d1550346662e8b458e/html5/thumbnails/4.jpg)
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.
![Page 5: Building Windows Services in VB](https://reader036.vdocuments.net/reader036/viewer/2022062512/5528f1d1550346662e8b458e/html5/thumbnails/5.jpg)
Design Guidelines
Consistency.
No popup messages.
Information/Errors need to be written to somewhere.EventLog.Database.
Be careful blocking.
![Page 6: Building Windows Services in VB](https://reader036.vdocuments.net/reader036/viewer/2022062512/5528f1d1550346662e8b458e/html5/thumbnails/6.jpg)
Types of .NET Applications
ASP.NET.Web Services.WinForms.Components.Windows
Services.Others.
![Page 7: Building Windows Services in VB](https://reader036.vdocuments.net/reader036/viewer/2022062512/5528f1d1550346662e8b458e/html5/thumbnails/7.jpg)
.NET Support for Services System.ServiceProcess namespace.
Inherit from the ServiceBase Class.
Installation.
ServiceController Class Allows communication from authorized user (WinForms, ASP.NET, or other) to a Service (thru SCM).
![Page 8: Building Windows Services in VB](https://reader036.vdocuments.net/reader036/viewer/2022062512/5528f1d1550346662e8b458e/html5/thumbnails/8.jpg)
Languages Support
C++ (Managed & Unmanaged).
Visual Basic.
C#.
Other .NET Languages.
![Page 9: Building Windows Services in VB](https://reader036.vdocuments.net/reader036/viewer/2022062512/5528f1d1550346662e8b458e/html5/thumbnails/9.jpg)
Parts of a .NET Windows Service
Service Control Manager (SCM).System.ServiceProcess.ServiceBase
classEvents.Installation.
Process Installation.Service Installation.
![Page 10: Building Windows Services in VB](https://reader036.vdocuments.net/reader036/viewer/2022062512/5528f1d1550346662e8b458e/html5/thumbnails/10.jpg)
Events in ServiceBase
OnStart().OnStop().OnPause().OnContinue().OnShutdown().OnPowerEvent().OnCustomCommand().
![Page 11: Building Windows Services in VB](https://reader036.vdocuments.net/reader036/viewer/2022062512/5528f1d1550346662e8b458e/html5/thumbnails/11.jpg)
OnStart() Event
Called when the Service is issued the start command.
VB Syntax:Protected Overridable Sub OnStart
( _ByVal args() as String )
Hard to Debug By Default.
![Page 12: Building Windows Services in VB](https://reader036.vdocuments.net/reader036/viewer/2022062512/5528f1d1550346662e8b458e/html5/thumbnails/12.jpg)
Debugging the OnStart() Event
Create a dummy service that is a part of your process.
Start the dummy service to start the process.
Attach to running process.Place breakpoint.Start “real” service.
![Page 13: Building Windows Services in VB](https://reader036.vdocuments.net/reader036/viewer/2022062512/5528f1d1550346662e8b458e/html5/thumbnails/13.jpg)
OnStop() Event
Called when the Service is issued the stop command.
Protected Overridable Sub OnStop().
![Page 14: Building Windows Services in VB](https://reader036.vdocuments.net/reader036/viewer/2022062512/5528f1d1550346662e8b458e/html5/thumbnails/14.jpg)
OnPause() Event
Called when the Service is issued the pause command.
Protected Overrideable Sub OnPause().
![Page 15: Building Windows Services in VB](https://reader036.vdocuments.net/reader036/viewer/2022062512/5528f1d1550346662e8b458e/html5/thumbnails/15.jpg)
OnContinue() Event
Called when the Service is issued the continue command.
Protected Overrideable Sub OnContinue().
![Page 16: Building Windows Services in VB](https://reader036.vdocuments.net/reader036/viewer/2022062512/5528f1d1550346662e8b458e/html5/thumbnails/16.jpg)
OnShutdown() Event
Called when the System sends the shutdown command to all Applications specifying that a system shutdown is inprogress.
Similar to the OnStop() event.
Protected Overrideable Sub OnShutdown().
![Page 17: Building Windows Services in VB](https://reader036.vdocuments.net/reader036/viewer/2022062512/5528f1d1550346662e8b458e/html5/thumbnails/17.jpg)
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
broadcast. True = Application is in a state where a suspend is ok. False
= suspend is not ok. PowerBroadcastStatus is an enumertion with 9 values.
![Page 18: Building Windows Services in VB](https://reader036.vdocuments.net/reader036/viewer/2022062512/5528f1d1550346662e8b458e/html5/thumbnails/18.jpg)
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.
![Page 19: Building Windows Services in VB](https://reader036.vdocuments.net/reader036/viewer/2022062512/5528f1d1550346662e8b458e/html5/thumbnails/19.jpg)
Security Context Service runs within a defined security context
(UserId/PassWord). 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).
![Page 20: Building Windows Services in VB](https://reader036.vdocuments.net/reader036/viewer/2022062512/5528f1d1550346662e8b458e/html5/thumbnails/20.jpg)
Custom Commands /ServiceController
Use the ServiceController class.ExecuteCommand( ByVal command as
Integer ) method.Start, Stop, Pause, Continue.MachineName, ServiceName, Status
properties.
![Page 21: Building Windows Services in VB](https://reader036.vdocuments.net/reader036/viewer/2022062512/5528f1d1550346662e8b458e/html5/thumbnails/21.jpg)
Installation
Each executable must have a process installer.
Each service within a process must have a service installer.
Command line utility (installutil.exe)
![Page 22: Building Windows Services in VB](https://reader036.vdocuments.net/reader036/viewer/2022062512/5528f1d1550346662e8b458e/html5/thumbnails/22.jpg)
App.Config
XML Format.
Excellent for read only information.
![Page 23: Building Windows Services in VB](https://reader036.vdocuments.net/reader036/viewer/2022062512/5528f1d1550346662e8b458e/html5/thumbnails/23.jpg)
Great, Now What canYou do with a Service?
Listen for events to occur.Network requests.Timer countdown.Messages arriving in a message
queue.File system changes.Other.
![Page 24: Building Windows Services in VB](https://reader036.vdocuments.net/reader036/viewer/2022062512/5528f1d1550346662e8b458e/html5/thumbnails/24.jpg)
Example
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.
![Page 25: Building Windows Services in VB](https://reader036.vdocuments.net/reader036/viewer/2022062512/5528f1d1550346662e8b458e/html5/thumbnails/25.jpg)
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 9.2.0.2.100.0 users, problem has been resolved)
![Page 26: Building Windows Services in VB](https://reader036.vdocuments.net/reader036/viewer/2022062512/5528f1d1550346662e8b458e/html5/thumbnails/26.jpg)
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.
![Page 27: Building Windows Services in VB](https://reader036.vdocuments.net/reader036/viewer/2022062512/5528f1d1550346662e8b458e/html5/thumbnails/27.jpg)
Things to look at / Last Thoughts
Event Processing vs. Blocking.EventLog.Multiple Threads of Execution.Weak References.Performance Monitor
Integration.Nothing wrong with Interop.
![Page 28: Building Windows Services in VB](https://reader036.vdocuments.net/reader036/viewer/2022062512/5528f1d1550346662e8b458e/html5/thumbnails/28.jpg)
Questions?
Scalable Development, Inc.Consulting & Development Services.http://www.scalabledevelopment.com865-693-3004.wallym@scalabledevelopment.com
END
Scalable Development, Inc.Building systems today that perform tomorrow.