Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Introduction To Windows Services


Published on

The talk I'm giving at CodeStock this year

  • Be the first to comment

  • Be the first to like this

Introduction To Windows Services

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