Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  .NET Newbie  »  Windows Services for VB.NET Developers  »  Deployment
 »  Home  »  Windows Development  »  Windows Services for VB.NET Developers  »  Deployment
Windows Services for VB.NET Developers
by Scott Rutherford | Published  12/20/2005 | .NET Newbie Windows Development | Rating:
Scott Rutherford
Scott Rutherford is a consultant and .NET developer who has developed Enterprise business applications in many fields including Restaurant, Market Research, Automotive, and Analytical Chemistry. He is an Microsoft Certified Professional with the .NET Platform. 

View all articles by Scott Rutherford...
Deployment

Once your Service and Worker classes are complete, compile the project to get your service EXE.

To install the service on a PC you can use the .NET frameworks tool InstallUtil.exe, found in the frameworks system directory.

You can copy the code listed here into a BAT file for simple DOS installation.

To uninstall just put a –U flag after the InstallUtil.exe call
(i.e. %FIRSTPART%%DOTNETVER%%SECONDPART% -U %PROG%).

[DOS]
@echo off
SET PROG="C:\EmptyService.exe"
SET FIRSTPART=%WINDIR%"\Microsoft.NET\Framework\v"
SET SECONDPART="\InstallUtil.exe"
SET DOTNETVER=2.0.50727
  IF EXIST %FIRSTPART%%DOTNETVER%%SECONDPART% GOTO install
SET DOTNETVER=1.1.4322
  IF EXIST %FIRSTPART%%DOTNETVER%%SECONDPART% GOTO install
SET DOTNETVER=1.0.3705
  IF EXIST %FIRSTPART%%DOTNETVER%%SECONDPART% GOTO install
GOTO fail
:install
  ECHO Found .NET Framework version %DOTNETVER%
  ECHO Installing service %PROG%
  %FIRSTPART%%DOTNETVER%%SECONDPART% %PROG%
  GOTO end
:fail
  echo FAILURE -- Could not find .NET Framework install
:end
  ECHO DONE!!!
  Pause

Some deployment hints:
  • Elevate NTFS on the service EXE file to ensure it is not replaced by another similar named exe which will now run under the configured security context.
  • To avoid a reboot after install/uninstall unlock resources used by your service, which includes closing files, closing the Windows Service Manager, etc. Sometimes adding an IISRESET line to the install script is helpful.

Now your service is installed. Start it and Stop it from the Service Manager and use the Event Log Viewer to view its output.



How would you rate the quality of this article?
1 2 3 4 5
Poor Excellent
Tell us why you rated this way (optional):

Article Rating
The average rating is: No-one else has rated this article yet.

Article rating:4.4 out of 5
 65 people have rated this page
Article Score44553
Comments    Submit Comment

Comment #1  (Posted by an unknown user on 01/04/2006)
Rating
Great!
 
Comment #2  (Posted by Shihab on 01/25/2006)
Rating
Very useful
 
Comment #3  (Posted by an unknown user on 01/28/2006)
Rating
I have been looking for an intro on Windows services. Thanks!
 
Comment #4  (Posted by an unknown user on 03/06/2006)
Rating
good one
 
Comment #5  (Posted by an unknown user on 03/23/2006)
Rating
Nice work!
 
Comment #6  (Posted by an unknown user on 03/23/2006)
Rating
Nice work!
 
Comment #7  (Posted by an unknown user on 03/28/2006)
Rating
Quite interesting.
 
Comment #8  (Posted by an unknown user on 03/29/2006)
Rating
good
 
Comment #9  (Posted by an unknown user on 03/29/2006)
Rating
good
 
Comment #10  (Posted by an unknown user on 04/28/2006)
Rating
Great Intro
 
Comment #11  (Posted by an unknown user on 05/15/2006)
Rating
Really great work. Thanks

I wonder if a windows service can consume a web service too ?
 
Comment #12  (Posted by an unknown user on 05/29/2006)
Rating
Sure a Service can consume a web service like any other Windows app. You need to ensure your security context will work -- i.e. the account that the Windows Service runs as must have ability to make web request (authenticate if necessary to web server, on proxy, etc.).
 
Comment #13  (Posted by an unknown user on 06/07/2006)
Rating
Great explanation
 
Comment #14  (Posted by an unknown user on 06/07/2006)
Rating
excellent! exactly what I needed.
 
Comment #15  (Posted by an unknown user on 07/18/2006)
Rating
most informative article i've found so far - at least it explains what i'm trying to achieve. shame the example is in vb though, such an ugly language. wish i could find more c# centred stuff...
 
Comment #16  (Posted by an unknown user on 09/14/2006)
Rating
Excellent!
 
Comment #17  (Posted by an unknown user on 09/15/2006)
Rating
Nice work!
 
Comment #18  (Posted by an unknown user on 10/02/2006)
Rating
Hi,
Nice project !
You can also uninstall a service using c:\winnt\SC.exe delete service1
Sc is part of the W2k Resource kit.
http://www.petri.co.il/download_free_reskit_tools.htm

Andy

 
Comment #19  (Posted by an unknown user on 10/23/2006)
Rating
I reviewed a dozen articles on the subject of building a service in vb.net, and this is the only one that actually works! Thanks a bunch Scott.
 
Comment #20  (Posted by an unknown user on 11/16/2006)
Rating
It works in 10 minutes :-)) Thanks
 
Comment #21  (Posted by an unknown user on 11/27/2006)
Rating
Just excellent! Thank you Scott
 
Comment #22  (Posted by Larry on 11/28/2006)
Rating
Hi, I cannot install this service. I compiled the code form this article and receiving an error message durring installaion: Unknown error "-1". Please help.

Thanks,
Larry
 
Comment #23  (Posted by an unknown user on 01/04/2007)
Rating
Excelent and simple! Thanks a lot.
 
Comment #24  (Posted by an unknown user on 01/18/2007)
Rating
Simple and to the point. Thank you.
 
Comment #25  (Posted by Doug on 01/23/2007)
Rating
As I have the "standard" edition, I can't seem to get this to work. It compiles & installs, but won't start, *immediately* throwing the "failed to start in a timely fashion."
What type of project must one start with, what does the project's property page look like?
 
Comment #26  (Posted by an unknown user on 02/12/2007)
Rating
what a childice example have you given.
 
Comment #27  (Posted by an unknown user on 02/19/2007)
Rating
How about some hint of "How to consume a win service/service Method form an application??
 
Comment #28  (Posted by [The author] on 02/19/2007)
Rating
RE; Comment #27: The article does present the concept of "OnCustomCommand" on page three. However, from your language I expect you are talking about Web Services which are clearly distinguished from Windows Services on page one.
 
Comment #29  (Posted by an unknown user on 03/12/2007)
Rating
Very helpful. Thanks!
 
Comment #30  (Posted by an unknown user on 05/03/2007)
Rating
I was looking for that installutil thingy... Thanks for the great input!
 
Comment #31  (Posted by an unknown user on 06/05/2007)
Rating
WS with timer explaination are many, with threads its really rare. This one is a good one out of those rare explainations.
 
Comment #32  (Posted by an unknown user on 06/06/2007)
Rating
Really good but needed debug process for development environment too.
 
Comment #33  (Posted by David on 06/07/2007)
Rating
Excellent explanations along with working code. The only questions I have left are how to get an icon in the system tray for this service and how to display a popup menu on a right click of that icon.
 
Comment #34  (Posted by an unknown user on 06/30/2007)
Rating
Thanks
 
Comment #35  (Posted by an unknown user on 07/21/2007)
Rating
Excellent understood very well keep it up and give us nice articles such as this
 
Comment #36  (Posted by Aakash on 09/06/2007)
Rating
This is my first exposure to Windows Services and the content was delivered very nicely, good work Scott.

(Comment #15) I do not digest why people hate VB, is it becuase it does those great things in simple manner where as its tough to achieve the same in their so called very good language ??
 
Comment #37  (Posted by Wardell Dye on 09/07/2007)
Rating
This was an excellent introduction to Windows Services as I was just given an assignment to preform a task to use Windows Services, great job!!!!!

Thanks
 
Comment #38  (Posted by an unknown user on 10/10/2007)
Rating
Perhaps I am missing something, but when I try to compile the code, I'm getting the following error: 'Sub Main' was not found in 'WindowsService1.Service1'. Do I need to change the Startup Object?

 
Comment #39  (Posted by an unknown user on 10/17/2007)
Rating
Compiled OK. Installed OK. Service Started...BUT...nothing in the event viewer when I start the service...
 
Comment #40  (Posted by Bobby on 11/29/2007)
Rating
When I try to start the service, I get this error. Any idea as to what could be happening?

Service cannot be started. System.ArgumentException: Delegate to an instance method cannot have null 'this'.
at System.MulticastDelegate.ThrowNullThisInDelegateToInstance()
at WindowsService.Service.OnStart(String[] args)
at System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback
 
Comment #41  (Posted by Bobby on 11/29/2007)
Rating
Sorry, my mistake with above error. Works perfectly now. Thanks a lot.
 
Comment #42  (Posted by an unknown user on 12/10/2007)
Rating
Followed instructions and re-checked. Service will not install, so something must have been left out

 
Comment #43  (Posted by an unknown user on 02/19/2008)
Rating
Great sample. Following your instructions it works at first execution. Thanks very much !!!
 
Comment #44  (Posted by an unknown user on 03/26/2008)
Rating
it worked as per my requirement..
 
Comment #45  (Posted by an unknown user on 05/20/2008)
Rating
Brilliant -- just what I needed.
 
Comment #46  (Posted by an unknown user on 05/23/2008)
Rating
Excellent Explanation, But i was not found that how to communicate with service prepared, from client machine
 
Comment #47  (Posted by an unknown user on 06/08/2008)
Rating
Useful, but a lot of information is missing like getting the installer to actually install the service. The example here will not work as it is written.
 
Comment #48  (Posted by an unknown user on 06/12/2008)
Rating
Fantastic look at a simple windows service.
 
Sponsored Links