Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  .NET Newbie  »  Windows Services for VB.NET Developers  »  Start the Project and Set Visual Properties (Visual Studio Designer)
 »  Home  »  Windows Development  »  Windows Services for VB.NET Developers  »  Start the Project and Set Visual Properties (Visual Studio Designer)
Windows Services for VB.NET Developers
by Scott Rutherford | Published  12/20/2005 | .NET Newbie Windows Development | Rating:
Start the Project and Set Visual Properties (Visual Studio Designer)

To get started, you'll need to create a Visual Studio .NET Project for your new Service. Select File | New Project from Visual Studio, then select Windows Service from the appropriate templates window (Note: in VB Standard edition this template is not available. However, the code listed in this article will still compile) – the name of your project will become the name of your executable but not necessarily the service. Once the project is started, right-click anywhere in the designer and select “Add Installer”. This adds a Project Installer file to your project with two components called during Service installation to configure Registry keys that affect your Windows Service. You should set all of the following properties of the ServiceInstaller and ServiceProcessInstaller components:

    ServiceInstaller.ServiceName:
    This is the registered name of the service. This name is used to start and stop the service from the command line (e.g. NET START EmptyService).

    ServiceInstaller.DisplayName:
    This is the long name of the service that will appear in the “Name” column of the Windows Service Manager dialog.

    ServiceInstaller.StartType:
    This is the default state of the service. Disabled means the service will not start, manual means that it can be started, and automatic means that it will start when the OS boots up. This can be changed administratively in the Service Manager dialog.

    ServiceProcessInstaller.Account:
    This is account under whose security context the Service will run. This can be changed administratively on the “Log On” tab of the Service Manager dialog. Setting this property to “User” will cause a prompt for username/password when the service is installed (Alternatively, ServiceProcessInstaller.Username and Password properties can be set from the code-behind for the Project Installer).

There are several other properties you may consider setting at this point.

    ServiceInstaller.ServicesDependedOn:
    This requires other services to be running before this service will start, and causes this service to stop if they stop.

    Description
    This is only available with .NET Fx 2.0 (although a low-level hack is available), sets the full description displayed in the Service Manager dialog.

The following properties of the Service itself are available from the Designer as well:

    AutoLog:
    When set to True, the service will automatically report Start, Stop, Pause and Continue commands to the Windows Application Event Log (custom commands are also logged).

    CanStop:
    Some OS services do not accept stop commands, and for these CanStop is False. In most cases, you want to accept a Stop command. If you do not implement OnStop method (see below) the method of the base class will be used.

    CanPauseAndContinue:
    If you want to enable pausing for your service, you must set this property True and implement the OnPause/OnContinue methods (see below). If this property is False, these methods do not get called. Pausing should allow processing to stop without full take-down and cleanup of Service resources.

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