Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

27.08.

2013

Creating a Basic Windows Service in C# - CodeProject


10,045,797 members (57,744 online) Sign in

home help

articles

quick answers

discussions

features

community
Search for articles, questions, tips

Articles General Reading Hardware & System Windows Services

Next

Article Browse Code Stats Revisions Alternatives Comments & Discussions (257)

Creating a Basic Windows Service in C#


By John Storer II, 5 Jun 2006
4.89 (176 votes)

About Article
A basic, easy-to-use template for a Windows Service in C# 2.0. Type Licence First Posted Views Bookmarked Article CPOL 5 Jun 2006 867,919 358 times

Download source - 3.78 Kb

Introduction
Do a search some time on Google for Services and C#, and you'll come across a lot of ASP.NET references for web services. Not very useful for a beginner looking for info on Windows Services! I've had this very problem, so I decided to do it myself, the old-fashioned way. This article in not intended to contend with anyone else's article. It is simply a basic skeleton of a Windows Service, nothing more. I know that in Visual Studio 2003, there was a template for a Windows Service, but I also remember it was rather obfuscated by Microsoft as far as templates go. Now that I use Visual C# 2005 Express Edition, there is no template for Windows Services. So, starting from there, I created one that works in .NET 2.0.

.NET2.0 Win2K WinXP Win2003 VS2005 C# Dev , +

The Process
Here's a small example on how to build a Windows Service in C#. First, load up your IDE of choice (mine is the free Visual C# 2005 Express Edition) and create an empty project. You can do this in VC# 2005 EE by clicking the menus: [File]->[New Project], Select "Empty Project", and name it what you will (mine is called WindowsService, original!). Click OK to create the project. Your project is created, and initially, it's not saved anywhere but a temporary location, so go ahead and go to [File]->[Save All], and okay through the dialog. This will officially save your project. Next, add the S y s t e m . S e r v i c e P r o c e s s and S y s t e m . C o n f i g u r a t i o n . I n s t a l lreferences to your project. In the Solution Explorer, right-click "References", and select "Add Reference...". In the dialog box that pops up, make sure the ".NET" tab is selected, and then scroll down in the list to "S y s t e m . S e r v i c e P r o c e s s ", select it, and click OK. You'll see a new entry under References. This will allow you to write code that references the S y s t e m . S e r v i c e P r o c e s s classes. Repeat the process to add the S y s t e m . C o n f i g u r a t i o n . I n s t a l lreference. Now, add two new class files to the project, one labeled: "WindowsService.cs " and the other: "WindowsServiceInstaller.cs ". In the Solution Explorer, right-click the project name and go to: [Add]->[Class]. Name the class "WindowsService.cs " and then hit OK. Repeat the process for "WindowsServiceInstaller.cs ". Now you have two brand-new files with the basics. In the "WindowsService.cs " class, copy the following:
Collapse | Copy Code

u s i n gS y s t e m ; u s i n gS y s t e m . D i a g n o s t i c s ; u s i n gS y s t e m . S e r v i c e P r o c e s s ; n a m e s p a c eW i n d o w s S e r v i c e { c l a s sW i n d o w s S e r v i c e:S e r v i c e B a s e { / / /< s u m m a r y > / / /P u b l i cC o n s t r u c t o rf o rW i n d o w s S e r v i c e .

Top News
1/6

www.codeproject.com/Articles/14353/Creating-a-Basic-Windows-Service-in-C

27.08.2013

Creating a Basic Windows Service in C# - CodeProject


/ / /-P u ta l lo fy o u rI n i t i a l i z a t i o nc o d eh e r e . / / /< / s u m m a r y > p u b l i cW i n d o w s S e r v i c e ( ) { t h i s . S e r v i c e N a m e=" M yW i n d o w sS e r v i c e " ; t h i s . E v e n t L o g . L o g=" A p p l i c a t i o n " ; / /T h e s eF l a g ss e tw h e t h e ro rn o tt oh a n d l et h a ts p e c i f i c / / t y p eo fe v e n t .S e tt ot r u ei fy o un e e di t ,f a l s eo t h e r w i s e . t h i s . C a n H a n d l e P o w e r E v e n t=t r u e ; t h i s . C a n H a n d l e S e s s i o n C h a n g e E v e n t=t r u e ; t h i s . C a n P a u s e A n d C o n t i n u e=t r u e ; t h i s . C a n S h u t d o w n=t r u e ; t h i s . C a n S t o p=t r u e ;

6 technologies you should learn this year


Get the Insider News free each morning.

Related Videos

/ / /< s u m m a r y > / / /T h eM a i nT h r e a d :T h i si sw h e r ey o u rS e r v i c ei sR u n . / / /< / s u m m a r y > s t a t i cv o i dM a i n ( ) { S e r v i c e B a s e . R u n ( n e wW i n d o w s S e r v i c e ( ) ) ; } / / /< s u m m a r y > / / /D i s p o s eo fo b j e c t st h a tn e e di th e r e . / / /< / s u m m a r y > / / /< p a r a mn a m e = " d i s p o s i n g " > W h e t h e r / / / o rn o td i s p o s i n gi sg o i n go n . < / p a r a m > p r o t e c t e do v e r r i d ev o i dD i s p o s e ( b o o ld i s p o s i n g ) { b a s e . D i s p o s e ( d i s p o s i n g ) ; } / / /< s u m m a r y > / / /O n S t a r t ( ) :P u ts t a r t u pc o d eh e r e / / / -S t a r tt h r e a d s ,g e ti n i t a ld a t a ,e t c . / / /< / s u m m a r y > / / /< p a r a mn a m e = " a r g s " > < / p a r a m > p r o t e c t e do v e r r i d ev o i dO n S t a r t ( s t r i n g [ ]a r g s ) { b a s e . O n S t a r t ( a r g s ) ; } / / /< s u m m a r y > / / /O n S t o p ( ) :P u ty o u rs t o pc o d eh e r e / / /-S t o pt h r e a d s ,s e tf i n a ld a t a ,e t c . / / /< / s u m m a r y > p r o t e c t e do v e r r i d ev o i dO n S t o p ( ) { b a s e . O n S t o p ( ) ; } / / /< s u m m a r y > / / /O n P a u s e :P u ty o u rp a u s ec o d eh e r e / / /-P a u s ew o r k i n gt h r e a d s ,e t c . / / /< / s u m m a r y > p r o t e c t e do v e r r i d ev o i dO n P a u s e ( ) { b a s e . O n P a u s e ( ) ; } / / /< s u m m a r y > / / /O n C o n t i n u e ( ) :P u ty o u rc o n t i n u ec o d eh e r e / / /-U n p a u s ew o r k i n gt h r e a d s ,e t c . / / /< / s u m m a r y > p r o t e c t e do v e r r i d ev o i dO n C o n t i n u e ( ) { b a s e . O n C o n t i n u e ( ) ; } / / /< s u m m a r y > / / /O n S h u t d o w n ( ) :C a l l e dw h e nt h eS y s t e mi ss h u t t i n gd o w n / / /-P u tc o d eh e r ew h e ny o un e e ds p e c i a lh a n d l i n g / / / o fc o d et h a td e a l sw i t has y s t e ms h u t d o w n ,s u c h / / / a ss a v i n gs p e c i a ld a t ab e f o r es h u t d o w n . / / /< / s u m m a r y > p r o t e c t e do v e r r i d ev o i dO n S h u t d o w n ( ) { b a s e . O n S h u t d o w n ( ) ; } / / /< s u m m a r y > / / /O n C u s t o m C o m m a n d ( ) :I fy o un e e dt os e n dac o m m a n dt oy o u r / / / s e r v i c ew i t h o u tt h en e e df o rR e m o t i n go rS o c k e t s ,u s e / / / t h i sm e t h o dt od oc u s t o mm e t h o d s . / / /< / s u m m a r y > / / /< p a r a mn a m e = " c o m m a n d " > A r b i t r a r yI n t e g e rb e t w e e n1 2 8&2 5 6 < / p a r a m > p r o t e c t e do v e r r i d ev o i dO n C u s t o m C o m m a n d ( i n tc o m m a n d ) { / / Ac u s t o mc o m m a n dc a nb es e n tt oas e r v i c eb yu s i n gt h i sm e t h o d : / / # i n tc o m m a n d=1 2 8 ;/ / S o m eA r b i t r a r yn u m b e rb e t w e e n1 2 8&2 5 6 / / # S e r v i c e C o n t r o l l e rs c=n e wS e r v i c e C o n t r o l l e r ( " N a m e O f S e r v i c e " ) ; / / # s c . E x e c u t e C o m m a n d ( c o m m a n d ) ; b a s e . O n C u s t o m C o m m a n d ( c o m m a n d ) ;

Related Articles
Implementing a Basic Hello World WCF Service Windows Services in Action I WCF: From a Beginner's perspective & a Tutorial Invoking a WCF Service from a CLR Trigger Simple Windows Service in C++ Install a Service using a Script SOAP Web Services: Create Once, Consume Everywhere Implementing a Basic Hello World WCF Service (v4.5) Simple Windows Service Sample A Self Contained NT Service Class: No Derivations Required Communication options with WCF - Part 1 Basic Authentication on a WCF REST Service Three ways to do WCF instance management WCF (Windows Communication Foundation) Introduction and Implementation A Windows Communication Foundation (WCF) Overview A Beginner's Tutorial for Understanding Windows Communication Foundation (WCF) Contract Model for Manageable Services How do I start a .NET WCF Windows Service automatically soon after the setup installation is complete Windows Indexing Service Basics Create RESTful WCF Service API: Step By Step Guide Windows Communication Foundation Basics

www.codeproject.com/Articles/14353/Creating-a-Basic-Windows-Service-in-C

2/6

27.08.2013
}

Creating a Basic Windows Service in C# - CodeProject

/ / /< s u m m a r y > / / /O n P o w e r E v e n t ( ) :U s e f u lf o rd e t e c t i n gp o w e rs t a t u sc h a n g e s , / / / s u c ha sg o i n gi n t oS u s p e n dm o d eo rL o wB a t t e r yf o rl a p t o p s . / / /< / s u m m a r y > / / /< p a r a mn a m e = " p o w e r S t a t u s " > T h eP o w e rB r o a d c a s tS t a t u s / / /( B a t t e r y L o w ,S u s p e n d ,e t c . ) < / p a r a m > p r o t e c t e do v e r r i d eb o o lO n P o w e r E v e n t ( P o w e r B r o a d c a s t S t a t u sp o w e r S t a t u s ) { r e t u r nb a s e . O n P o w e r E v e n t ( p o w e r S t a t u s ) ; } / / /< s u m m a r y > / / /O n S e s s i o n C h a n g e ( ) :T oh a n d l eac h a n g ee v e n t / / / f r o maT e r m i n a lS e r v e rs e s s i o n . / / / U s e f u li fy o un e e dt od e t e r m i n e / / / w h e nau s e rl o g si nr e m o t e l yo rl o g so f f , / / / o rw h e ns o m e o n el o g si n t ot h ec o n s o l e . / / /< / s u m m a r y > / / /< p a r a mn a m e = " c h a n g e D e s c r i p t i o n " > T h eS e s s i o nC h a n g e / / /E v e n tt h a to c c u r e d . < / p a r a m > p r o t e c t e do v e r r i d ev o i dO n S e s s i o n C h a n g e ( S e s s i o n C h a n g e D e s c r i p t i o nc h a n g e D e s c r i p t i o n ) { b a s e . O n S e s s i o n C h a n g e ( c h a n g e D e s c r i p t i o n ) ; }

In the "WindowsServiceInstaller.cs " class, copy the following:


Collapse | Copy Code

u s i n gS y s t e m ; u s i n gS y s t e m . C o m p o n e n t M o d e l ; u s i n gS y s t e m . C o n f i g u r a t i o n . I n s t a l l ; u s i n gS y s t e m . S e r v i c e P r o c e s s ; n a m e s p a c eW i n d o w s S e r v i c e { [ R u n I n s t a l l e r ( t r u e ) ] p u b l i cc l a s sW i n d o w s S e r v i c e I n s t a l l e r:I n s t a l l e r { / / /< s u m m a r y > / / /P u b l i cC o n s t r u c t o rf o rW i n d o w s S e r v i c e I n s t a l l e r . / / /-P u ta l lo fy o u rI n i t i a l i z a t i o nc o d eh e r e . / / /< / s u m m a r y > p u b l i cW i n d o w s S e r v i c e I n s t a l l e r ( ) { S e r v i c e P r o c e s s I n s t a l l e rs e r v i c e P r o c e s s I n s t a l l e r= n e wS e r v i c e P r o c e s s I n s t a l l e r ( ) ; S e r v i c e I n s t a l l e rs e r v i c e I n s t a l l e r=n e wS e r v i c e I n s t a l l e r ( ) ; / / #S e r v i c eA c c o u n tI n f o r m a t i o n s e r v i c e P r o c e s s I n s t a l l e r . A c c o u n t=S e r v i c e A c c o u n t . L o c a l S y s t e m ; s e r v i c e P r o c e s s I n s t a l l e r . U s e r n a m e=n u l l ; s e r v i c e P r o c e s s I n s t a l l e r . P a s s w o r d=n u l l ; / / #S e r v i c eI n f o r m a t i o n s e r v i c e I n s t a l l e r . D i s p l a y N a m e=" M yN e wC #W i n d o w sS e r v i c e " ; s e r v i c e I n s t a l l e r . S t a r t T y p e=S e r v i c e S t a r t M o d e . A u t o m a t i c ; / / #T h i sm u s tb ei d e n t i c a lt ot h eW i n d o w s S e r v i c e . S e r v i c e B a s en a m e / / #s e ti nt h ec o n s t r u c t o ro fW i n d o w s S e r v i c e . c s s e r v i c e I n s t a l l e r . S e r v i c e N a m e=" M yW i n d o w sS e r v i c e " ; t h i s . I n s t a l l e r s . A d d ( s e r v i c e P r o c e s s I n s t a l l e r ) ; t h i s . I n s t a l l e r s . A d d ( s e r v i c e I n s t a l l e r ) ;

The S e r v i c e A c c o u n tsection of the code is important for the security context under which your code will run. For every enumeration except U s e r , set the U s e r n a m e and P a s s w o r dproperties to n u l l . You can set the A c c o u n tproperty to the following (this is mostly from Microsoft's help file): The L o c a l S y s t e menumeration defines a highly privileged account, but most services do not require such an elevated privilege level. The L o c a l S e r v i c e enumeration provides a lower privilege level for the security context. The N e t w o r k S e r v i c eenumeration provides extensive local privileges, and provides the computer's credentials to remote servers. The U s e renumeration allows the use of a username and password combination to set the privilege level for the security context to that of a specified user. There. That wasn't so tough! Now, in the W i n d o w s S e r v i c e class, depending on what you're creating this service for, you'll probably want to include a background working thread to handle, well, the work. I've used this code simply by leaving everything as-is with just the
www.codeproject.com/Articles/14353/Creating-a-Basic-Windows-Service-in-C 3/6

27.08.2013

Creating a Basic Windows Service in C# - CodeProject

O n C u s t o m C o m m a n dmethod modified, and it works OK. If you wish to handle constant or timed
processing, best add at least one working thread. That's as far as I'll go in this article on setting up a Windows Service: everything else is pretty much up to the programmer. Now, the easy/hard part: Installation.

Installation
To install a service, you could create an installation program that encapsulates the executable for deployment, which I find time consuming and inefficient when testing an application. Or, you could install the service though the InstallUtil.exe process that comes with the .NET Framework. I created a simple batch file, install.bat , to simplify installation (and therefore testing) of the service. The batch script is shown below; uninstallation is as simple as creating another batch file, uninstall.bat , with exactly the same script, only substituting the /i (for install) with /u (for uninstall).
Collapse | Copy Code

@ E C H OO F F R E MT h ef o l l o w i n gd i r e c t o r yi sf o r. N E T2 . 0 s e tD O T N E T F X 2 = % S y s t e m R o o t % \ M i c r o s o f t . N E T \ F r a m e w o r k \ v 2 . 0 . 5 0 7 2 7 s e tP A T H = % P A T H % ; % D O T N E T F X 2 % e c h oI n s t a l l i n gW i n d o w s S e r v i c e . . . e c h oI n s t a l l U t i l/ iW i n d o w s S e r v i c e . e x e e c h oe c h oD o n e .

Points of Interest
I have noticed that other examples on creating services (when I can find them) are more focused on everything *but* the actual creation and structure of a Windows service. Since I did this mainly for a work project, I had to learn this all on my own, which in itself is rewarding, but I can imagine how other people feel when they need this kind of code as soon as possible and don't have the time to research it. Hopefully, this will help others who need just the basics and can go from there.

License
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author


John Storer II
Software Developer Employer's Security United States

I'm a Computer Support Technician for a Indiana School System. Really, I'm a C#/ASP.NET Developer in a Software Support coat. Jack-of-all-trades, I do Hardware Support, Software Support, Programming in C#.NET, ASP.NET / Javascript, Web Design, Graphic Arts and lots of other stuff. I've been programming almost 21 years now, and have used more than 10 different languages in the past.

Article Top

www.codeproject.com/Articles/14353/Creating-a-Basic-Windows-Service-in-C

4/6

27.08.2013

Creating a Basic Windows Service in C# - CodeProject


3 Tw eet 5

Sign Up to vote Poor

Excellent

Vote

Comments and Discussions


You must Sign In to use this message board. Search this forum Profile popups Spacing Relaxed Noise Very High Layout Open All Per page 10 Update First Prev Next Go

General feedback

Member 8362427

19-Jul-13 4:30

Excellent article - short and to the point - excellent


Sign In View Thread Permalink 5.00/5 (1 vote) michael78244 18-Jul-13 3:21

My vote of 5

This is exactly what I was looking for. It works great! Thank you!
Sign In View Thread Permalink

My vote of 5

Edo Tzumer

30-Jun-13 5:02

Great simple intro, albeit INHO it should be tagged as a Tip rather than an article
Sign In View Thread Permalink

My vote of 3
It's very Simple
Sign In View Thread Permalink

Inba karthik

7-May-13 1:19

My vote of 4

Symbios

30-Apr-13 12:19

well thought out scaffolding for laying out a windows service.


Sign In View Thread Permalink

My vote of 3
s
Sign In View Thread Permalink

Avdhesh kumar

4-Mar-13 22:06

My vote of 5
Super simple, straightforward and it works!
Sign In View Thread Permalink

Model12

30-Jan-13 9:41

Find a new job


www.codeproject.com/Articles/14353/Creating-a-Basic-Windows-Service-in-C

Kenneth Bullock

29-Dec-12 21:47

5/6

27.08.2013

Creating a Basic Windows Service in C# - CodeProject

Because your f***ing sh*t does not work sir. Good day.
Sign In View Thread Permalink

Re: Find a new job


What the mood?
Sign In View Thread Permalink

supernorb

15-Jun-13 8:10

My vote of 5
simple to understand explanation.
Sign In View Thread Permalink

Member 8927578

28-Sep-12 6:09

5.00/5 (1 vote)

Last Visit: 31-Dec-99 18:00 General News

Last Update: 26-Aug-13 21:39 Suggestion Question

Refresh Bug

1 2 3 4 5 6 7 8 9 10 11 Next Answer Joke Rant Admin

Permalink | Advertise | Privacy | Mobile Web02 | 2.6.130823.1 | Last Updated 5 Jun 2006

Layout: fixed | fluid

Article Copyright 2006 by John Storer II Everything else Copyright CodeProject, 1999-2013 Terms of Use

www.codeproject.com/Articles/14353/Creating-a-Basic-Windows-Service-in-C

6/6

You might also like