introduction to windows services

22
Introduction to Windows Services (Easier then you think) Josef Finsel AzureDBA.com

Upload: josef-finsel

Post on 17-Dec-2014

1.028 views

Category:

Entertainment & Humor


1 download

DESCRIPTION

The talk I'm giving at CodeStock this year

TRANSCRIPT

Page 1: Introduction To Windows Services

Introduction to Windows Services (Easier then you think)

Josef FinselAzureDBA.com

Page 2: Introduction To Windows Services

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

What’s a Service

• Long running program• Runs when computer starts• Can specify security context

Page 4: Introduction To Windows Services

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

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

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

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

Demo 1

• Using the Win Form and the Class Library

Page 9: Introduction To Windows Services

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

Reviewing Service Code

Page 11: Introduction To Windows Services

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

Service Process Installer

• Set Security Context

Page 13: Introduction To Windows Services

Installing the Service

• InstallUtil (Visual Studio Command Prompt)

• Provide account context if required

Page 14: Introduction To Windows Services

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

Running and Debugging a Service Demo

Page 16: Introduction To Windows Services

Service States

• OnStart enable timer• OnStop disable timer• OnShutdown disable timer• OnPause disable timer• OnContinue enable timer

Page 17: Introduction To Windows Services

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

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

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

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

Questions?

Page 22: Introduction To Windows Services

Contact me

• http://www.azuredba.com• Twitter: @carpdeus