Windows Services in .NET
page 6 of 10
by Kavitha Pradeep
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 42192/ 80

Building Windows Services

A windows service exists as an executable file and may contain more than one service. The behavior of each of the service is defined by writing a class for each that inherits from the ServiceProcess.ServiceBase class and code can be added to handle various methods provided by this class. The service can provide code for OnStart, OnStop, OnPause, OnContinue, and OnShutdown methods called by the SCM as a user interacts with the service. None of these procedures are required, but they can be used to provide specific behavior in reaction to requests from the SCM’s user interface, or programmatically from other services.

The steps to be taken to create windows services under .NET:

1.      Create a new project in .NET by choosing the ‘Windows Services’ template. The project can either be VB.NET or C#.

2.      A class named Service1 will be created. Open the class in design view. This can be done either by pressing Shift+F7 or choose View | Designer menu item.

3.      The properties could be set as necessary. The three Boolean properties – CanPauseAndContinue, CanShutDown, and CanStop- control the behavior of the service. It should look like this:

4.      Add startup code. The service’s OnStart event can be used to set up the service and data structures used, and perhaps log information to the event log. Also code to handle the OnContinue, OnCustomCommand, OnPause, OnShutdown, and OnStop events.

Add an Installer. This can be done by going to the service class’s design view as done in step 2. Now, clicking on the ‘Add Installer’ will add the installer classes.

Once the Installer class has been added, Visual Studio .NET creates a new project file named ProjectInstaller.cs. This file contains two components: ServiceProcessInstaller1 and ServiceInstaller1. The properties of the ServiceInstaller1 class will show the service name in ServiceName property as Service1. The ServiceProcessInstaller1 has properties such as UserName and Password as shown in fig 2.0. They will be by default set to ‘Nothing.’ For the installer to be able to install the service, the account information should be supplied. For a simple service the account property of the ServiceProcessInstaller1 could be set to LocalSystem. The ServiceName property of the ServiceInstaller object should be the same name as that of the service class.

For a simple service all the default behavior could be accepted and the service will install fine. By default the service class’s StartType property is set to Manual as shown in fig 2.1. If the service needs to start automatically when installed, the StartType property of the ServiceInstaller1 component in ProjectInstaller to Automatic.

                                                            Fig 2.0

                                                            Fig 2.1


View Entire Article

User Comments

Title: Windows Services   
Name: S.B.krishna Reddy
Date: 2010-12-31 4:29:52 AM
Comment:
The images u have presented in the web page are not proper and not showing the clear view
Title: Windows Service in .NET 2   
Name: sbanik
Date: 2009-10-14 6:24:58 AM
Comment:
\
Title: Windows Services in .NET   
Name: Nanthakumar
Date: 2008-10-16 6:53:15 AM
Comment:
Hi

This is Very Useful my project..

thanks
Title: Extremely helpful, even after 4 yrs~   
Name: Rajeev
Date: 2008-06-12 4:40:13 AM
Comment:
Thanks a lot for this article. It's been 4 yrs since it was authored, but yet it is so useful for beginners.

Thanks a ton.
Title: Learned Windows services   
Name: Debasmit Samal
Date: 2008-01-23 1:16:47 AM
Comment:
Hi

Thanks a lot and lot for the article.
Really i got basic concepts for the windows services.

Thanks again for such a nice article.
Title: Windows Service   
Name: Rohini S
Date: 2008-01-22 3:28:06 AM
Comment:
It is an excellent tutorial. It covers basics of Windows service. It is a good for beginners
Title: Crystal report autogeneration using windows services   
Name: Rajesh m
Date: 2007-08-09 9:03:43 AM
Comment:
Hi All,
I developed windows service which will daily at 12:00pm generate crystalreports and mail is sent to the mail with crystal report attached. Here my problem is it works fine in my system..but when i put in testing server it is not working...
Title: windows service   
Name: pawar megha p.
Date: 2007-05-03 5:00:26 AM
Comment:
Hellow!
very good article for windows services.i have problem for writting the Installutil.exe in command propmt of vs.it gives no file found,so what may be the problem?


thanks..
Title: Windows Service   
Name: M.Mansoor AliKhan
Date: 2006-11-13 2:32:37 AM
Comment:
This is really a knowledge providing article.

Thanks ..
Title: Running Windows Service from Web Service   
Name: Shantanu Karmakar
Date: 2006-08-18 6:19:41 AM
Comment:
Hi,
I'm having a problem to start and stop a windows service from my web service. It's giving an exception InvalidIOException - Couldn't strat service. Can any one help in this regard? I think the problem is with the security.
Title: How do we programmatically configure a Windows Service to interact with the desktop?   
Name: Shashi Kumar
Date: 2006-07-29 9:15:19 AM
Comment:
Hello,
Can u please let me know how do I make the windows service interact with the desktop since the windows service has to invoke a windows application upon its successful start?
Please let me know where exactly a property called "AllowServiceToInteractWithDesktop" exists and how do I use it?

I have been struck for quite sometime now... and any help would be greatly appreciated.

Feel free to mail me at shashi.satya@gmail.com

Thanks in advance.
Title: Webservice   
Name: Mahendra
Date: 2006-07-19 1:58:51 AM
Comment:
this is really a knowledge providing article.
thanks
Title: User Interface   
Name: Uli
Date: 2006-06-20 8:14:30 PM
Comment:
"Although it’s possible, Windows Service typically has no user interface."

Hi, great article, how can I find more information about creating a windows service with windows interface, so far everytime that my windows service timer goes off and executes some code, all the functionality will work as needed but because it won't show the form that I developed (This is actually my biggest problem), it will limit my application's functionality.

Any ideas???

If you guys have something please let me know: ulisesr@mvrmc.org

THANKS!
Title: windows services   
Name: shan
Date: 2006-05-08 10:45:38 AM
Comment:
hi
this explaination is good but is this possible to install the windows services without the use of installutl i mean calling just the function install() , i tried it but its giving "no object reference " problem i think the problem is with the hashtable may be its not having the proper context?
i need ur help
Title: Windows Service Behaviour   
Name: Deepak
Date: 2006-04-27 7:28:58 AM
Comment:
Hello, I have problem with Windows Service that I have created in VB.Net its behaviour is really strange i.e.Sometimes after Istallation and Start it runs but sometimes it just stucks somewhere.Then again this service needs to be re-installed(not just restart) then it runs perfectly very well.
This service does some Asynchronous task using delegates.
I could not understood this behaviour,let me know how can I understand it or what could be reasons.

--Deepak.
Title: windows services   
Name: shravan
Date: 2006-04-03 4:24:51 AM
Comment:
this is ok
now whether this is possible to install this service without installutil.exe i mean just adding entries in the registry by urself
well i am a newbie






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-02-25 3:54:38 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search