Creating Windows Service using ASP.NET
Good morning to All
Now I am going to explain how to Create a windows Service in asp.net
What is the purpose of Creating windows service in asp.net ?
For ex: if you have the task of Sending birthday email to the members Automatically or you are going to do some manipulation on particular day or do some manipulation on Consecutive intervals so at this situation you can go for Window service or even you can do birth email process through Sql Job Scheduling.
Now see how to create a simple service.
Choose Create Project -> VisualC# -> Windows -> Windows Service
Fig 1:
So once you give the windowService Name it will show this - by default the serviceName is Service1 here i am changed to TimerService.
Fig 2:
Then Right Click on Service - Select -> Add Installer
Fig 3:
Then the ProjectInstaller window show two Services like serviceProcessIntaller1 , serviceInstaller1 so you have to change some Properties for this service
Properties of serviceProcessIntaller1
Account - > there are some 4 types of account are there as per the requirement
1) LocalService - for only particular computer
2) Network Service - to access resource on remote side ie: server
3) Local System - to works on local System
4) User - Based on User credentials
For Brief Description Check it :
http://technet.microsoft.com/en-us/library/ms143504.aspx
Fig 4:
Properties of serviceInstaller1
StartType :
1) Automatic
2) Manual
3) Disabled
You have to specify whether you are going to start this Service Manually or Automatically or Disabled the Service.then give the name for ServiceName Property , And give some Name for DisplayName property - this name will be displayed on your servicelist to identify your Service.
Fig 5:
Now we going to write the code for Service on Service1.cs
by default it has OnStart and OnStop Event - so this event occurs when service start and stop.
using System.Timers;
Add timer to do some manipulation on particular intervals
//Initialize the timer
Timer timer = new Timer();
so here is the code - what i am doing is create a method called AddToFile where i am adding the string to the file ie: on C Directory if the file is present it will write it on or it will create a new file with the specified name and write the content on that file.
And onStart Event i am creating the Timer Elapsed event which called at every 1 minute so their i am adding another entry on that event.
Finally onStop Event i set the timer enabled = false and made last entry ie: Service stopped.
so once you written the code just press F5 to run it show this dialog
Fig 6:
so you can start the service to start the service you have to install installutil.exe through commandline.
so go to Command Prompt on this Path.
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727
From here you have to install the service , once you compile the project exe file is created on bin folder.
here is the step to install the Service
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727: InstallUtil "F:\WindowsService1\bin\Debug\WindowsService1.exe"
so if you press enter. Service will be installed.
To UnInstall The service follow this line
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\: IntsallUtil /u"F:\WindowsService1\bin\Debug\WindowsService1.exe"
Once the Serice is installed it will be available on your Service list To check this follow this one
Start -> Control Panel -> Administrative Tools -> Services -> then find the Service as you given while creating the windowsService ie: DisplayName : TimerService
Fig7:
so if you set the Startup Type - Automatic - the service started when the System boots, you can also start the service Manually Right Click on the Service -> start. to start the service.
After the start the service check the C:\ Directory - you can check the file with the entry.
Happy Coding.
Now I am going to explain how to Create a windows Service in asp.net
What is the purpose of Creating windows service in asp.net ?
For ex: if you have the task of Sending birthday email to the members Automatically or you are going to do some manipulation on particular day or do some manipulation on Consecutive intervals so at this situation you can go for Window service or even you can do birth email process through Sql Job Scheduling.
Now see how to create a simple service.
Choose Create Project -> VisualC# -> Windows -> Windows Service
Fig 1:
So once you give the windowService Name it will show this - by default the serviceName is Service1 here i am changed to TimerService.
Fig 2:
Then Right Click on Service - Select -> Add Installer
Fig 3:
Then the ProjectInstaller window show two Services like serviceProcessIntaller1 , serviceInstaller1 so you have to change some Properties for this service
Properties of serviceProcessIntaller1
Account - > there are some 4 types of account are there as per the requirement
1) LocalService - for only particular computer
2) Network Service - to access resource on remote side ie: server
3) Local System - to works on local System
4) User - Based on User credentials
For Brief Description Check it :
http://technet.microsoft.com/en-us/library/ms143504.aspx
Fig 4:
Properties of serviceInstaller1
StartType :
1) Automatic
2) Manual
3) Disabled
You have to specify whether you are going to start this Service Manually or Automatically or Disabled the Service.then give the name for ServiceName Property , And give some Name for DisplayName property - this name will be displayed on your servicelist to identify your Service.
Fig 5:
Now we going to write the code for Service on Service1.cs
by default it has OnStart and OnStop Event - so this event occurs when service start and stop.
using System.Timers;
Add timer to do some manipulation on particular intervals
//Initialize the timer
Timer timer = new Timer();
so here is the code - what i am doing is create a method called AddToFile where i am adding the string to the file ie: on C Directory if the file is present it will write it on or it will create a new file with the specified name and write the content on that file.
And onStart Event i am creating the Timer Elapsed event which called at every 1 minute so their i am adding another entry on that event.
Finally onStop Event i set the timer enabled = false and made last entry ie: Service stopped.
protected override void OnStart(string[] args) { // TODO: Add code here to start your service. //add line to the file AddToFile("Make starting service"); //ad 1: handle Elapsed event timer.Elapsed += new ElapsedEventHandler(OnElapsedTime); //ad 2: set interval to 1 minute (= 60,000 milliseconds) timer.Interval = 60000; //ad 3: enabling the timer timer.Enabled = true; } protected override void OnStop() { // TODO: Add code here to perform any tear-down necessary to stop your service. timer.Enabled = false; AddToFile("Make stopping service"); } private void AddToFile(string contents) { //set up a filestream FileStream fs = new FileStream(@"c:\timelog.txt", FileMode.OpenOrCreate, FileAccess.Write); //set up a streamwriter for adding text StreamWriter sw = new StreamWriter(fs); //find the end of the underlying filestream sw.BaseStream.Seek(0, SeekOrigin.End); //add the text sw.WriteLine(contents); //add the text to the underlying filestream sw.Flush(); //close the writer sw.Close(); } private void OnElapsedTime(object source, ElapsedEventArgs e) { AddToFile("Make Another entry"); }
so once you written the code just press F5 to run it show this dialog
Fig 6:
so you can start the service to start the service you have to install installutil.exe through commandline.
so go to Command Prompt on this Path.
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727
From here you have to install the service , once you compile the project exe file is created on bin folder.
here is the step to install the Service
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727: InstallUtil "F:\WindowsService1\bin\Debug\WindowsService1.exe"
so if you press enter. Service will be installed.
To UnInstall The service follow this line
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\: IntsallUtil /u"F:\WindowsService1\bin\Debug\WindowsService1.exe"
Once the Serice is installed it will be available on your Service list To check this follow this one
Start -> Control Panel -> Administrative Tools -> Services -> then find the Service as you given while creating the windowsService ie: DisplayName : TimerService
Fig7:
so if you set the Startup Type - Automatic - the service started when the System boots, you can also start the service Manually Right Click on the Service -> start. to start the service.
After the start the service check the C:\ Directory - you can check the file with the entry.
Happy Coding.
Read more »