Download - Introduction To Windows Services
![Page 1: Introduction To Windows Services](https://reader033.vdocument.in/reader033/viewer/2022061210/5490b061b47959f03e8b458d/html5/thumbnails/1.jpg)
Introduction to Windows Services (Easier then you think)
Josef FinselAzureDBA.com
![Page 2: Introduction To Windows Services](https://reader033.vdocument.in/reader033/viewer/2022061210/5490b061b47959f03e8b458d/html5/thumbnails/2.jpg)
Who am I?
• Database Administrator• C# Programmer• Author/Maintainer of a half
dozen Services used daily on my job
![Page 3: Introduction To Windows Services](https://reader033.vdocument.in/reader033/viewer/2022061210/5490b061b47959f03e8b458d/html5/thumbnails/3.jpg)
What’s a Service
• Long running program• Runs when computer starts• Can specify security context
![Page 4: Introduction To Windows Services](https://reader033.vdocument.in/reader033/viewer/2022061210/5490b061b47959f03e8b458d/html5/thumbnails/4.jpg)
Services Have Bad Rep
• Supposedly Services are:– Hard to program– Hard to debug
• Only true if you let them be true
![Page 5: Introduction To Windows Services](https://reader033.vdocument.in/reader033/viewer/2022061210/5490b061b47959f03e8b458d/html5/thumbnails/5.jpg)
Windows Services the Easy Way
• Solution with three projects:– Class Library that does all of the
work– Win Form app for debugging– Actual service that is nothing more
than a wrapper for the Class Library• Every line of code excluded from
the service is one less line to debug the hard way
![Page 6: Introduction To Windows Services](https://reader033.vdocument.in/reader033/viewer/2022061210/5490b061b47959f03e8b458d/html5/thumbnails/6.jpg)
Class Library Project
• Contains no Config information• Does no logging
– Offers a callback to communicate with calling program for logging
• Includes property for stopping long-running processes
• Should handle all errors
![Page 7: Introduction To Windows Services](https://reader033.vdocument.in/reader033/viewer/2022061210/5490b061b47959f03e8b458d/html5/thumbnails/7.jpg)
Win Form Application Project
• App.Config contains all necessary configuration for the Class
• Instantiates the class using only App.Config entries
• Outputs all log information to screen
• Has Timer, Checkbox to control Timer, Button to run without Timer and Button to test Stopping
![Page 8: Introduction To Windows Services](https://reader033.vdocument.in/reader033/viewer/2022061210/5490b061b47959f03e8b458d/html5/thumbnails/8.jpg)
Demo 1
• Using the Win Form and the Class Library
![Page 9: Introduction To Windows Services](https://reader033.vdocument.in/reader033/viewer/2022061210/5490b061b47959f03e8b458d/html5/thumbnails/9.jpg)
Windows Service Project
• Supports Service Functions• App.Config contains all necessary
configuration for the Class• Instantiates the class using only
App.Config entries• Outputs all log information to
screen• Has Timer• Copy of code from Win Form
![Page 10: Introduction To Windows Services](https://reader033.vdocument.in/reader033/viewer/2022061210/5490b061b47959f03e8b458d/html5/thumbnails/10.jpg)
Reviewing Service Code
![Page 11: Introduction To Windows Services](https://reader033.vdocument.in/reader033/viewer/2022061210/5490b061b47959f03e8b458d/html5/thumbnails/11.jpg)
Service Installer
• Class used for installing a service• Right-click on the Service
Component Window and select Add Installer
• Set the– Description– Display name– Start type– Other service dependencies
![Page 12: Introduction To Windows Services](https://reader033.vdocument.in/reader033/viewer/2022061210/5490b061b47959f03e8b458d/html5/thumbnails/12.jpg)
Service Process Installer
• Set Security Context
![Page 13: Introduction To Windows Services](https://reader033.vdocument.in/reader033/viewer/2022061210/5490b061b47959f03e8b458d/html5/thumbnails/13.jpg)
Installing the Service
• InstallUtil (Visual Studio Command Prompt)
• Provide account context if required
![Page 14: Introduction To Windows Services](https://reader033.vdocument.in/reader033/viewer/2022061210/5490b061b47959f03e8b458d/html5/thumbnails/14.jpg)
Important Caveat
• Do not recompile after the Installer information has changed without uninstalling first!
• Uninstall is same as install but with –u
• Failure to Uninstall leads to RegEdit
![Page 15: Introduction To Windows Services](https://reader033.vdocument.in/reader033/viewer/2022061210/5490b061b47959f03e8b458d/html5/thumbnails/15.jpg)
Running and Debugging a Service Demo
![Page 16: Introduction To Windows Services](https://reader033.vdocument.in/reader033/viewer/2022061210/5490b061b47959f03e8b458d/html5/thumbnails/16.jpg)
Service States
• OnStart enable timer• OnStop disable timer• OnShutdown disable timer• OnPause disable timer• OnContinue enable timer
![Page 17: Introduction To Windows Services](https://reader033.vdocument.in/reader033/viewer/2022061210/5490b061b47959f03e8b458d/html5/thumbnails/17.jpg)
When is a Service Frozen?
• If Windows initiates a State Change and gets no response in 30 seconds, it’s frozen and may get killed
![Page 18: Introduction To Windows Services](https://reader033.vdocument.in/reader033/viewer/2022061210/5490b061b47959f03e8b458d/html5/thumbnails/18.jpg)
When a Service Fails
• When a service fails you can:– Do nothing– Restart it– Run a program– Restart the computer
![Page 19: Introduction To Windows Services](https://reader033.vdocument.in/reader033/viewer/2022061210/5490b061b47959f03e8b458d/html5/thumbnails/19.jpg)
Deploying a Service
• Deploy in stages:– Running WinForm on Desktop in
Debug mode to test stability– Running as a Service on your box
with Debugger Attached– Deploying as real service
![Page 20: Introduction To Windows Services](https://reader033.vdocument.in/reader033/viewer/2022061210/5490b061b47959f03e8b458d/html5/thumbnails/20.jpg)
Best Practices
• Service = Lightweight Wrapper around class
• Class has callback for logging• Class has cancel mechanism• Most debugging done in WinForm
![Page 21: Introduction To Windows Services](https://reader033.vdocument.in/reader033/viewer/2022061210/5490b061b47959f03e8b458d/html5/thumbnails/21.jpg)
Questions?