Window service is a service which runs in a background and need no user interaction. It automatically started when computer boots.
Mostly, There is business requirement for long-running scheduled jobs based on some time interval. For example - Sending emails, Reminder messages etc after some time interval or on daily basis. So, Window service is best fit for this system.
To create window service
Add window service in your project
By default it will create following files as in below image in your window service project.
Now on serviceInstaller1 box right click and select properties. You will see property window on right side of screen. It contain basic properties of your service installation. Look at start-type which is manual. It means service will not start automatically after installation. You have to start it manually.
So far, We have 3 files in our window service project :
Mostly, There is business requirement for long-running scheduled jobs based on some time interval. For example - Sending emails, Reminder messages etc after some time interval or on daily basis. So, Window service is best fit for this system.
To create window service
Add window service in your project
By default it will create following files as in below image in your window service project.
- Program.cs basically have code to start your service that is Service1.cs.
- Service1.cs will contain OnStart and OnEnd method of service.
Now we required installer class to install service. To add installer class, click on Service1.cs. It will open a window right click on it and select AddInstaller as in below image.
It will add installer class named PprojectInstaller.cs.
So far, We have 3 files in our window service project :
- Program.cs, Which contain code to run your Service1.cs file.
- Service.cs, It contain method OnStart and OnEnd which will trigger when service start and end.
- ProjectInstaller.cs, Which is your installer class and used to install service.
Now lets add a another class to logging triggers named Liberary.cs
Liberary.cs
public static class Liberary
{
public static void WriteLog(String msg)
{
StreamWriter sw = null;
try
{
sw = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "\\LogFile.txt", true);
sw.WriteLine(DateTime.Now.ToString() + ": " + msg);
sw.Flush();
sw.Close();
}
catch { }
}
}
Service1.cs
public partial classService1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
Liberary.WriteLog("Service started");
Job();
}
public void Job()
{
Liberary.WriteLog(DateTime.Now + " : Test");
double TimerInterVal = (double)30000; //After 30 seconds
System.Timers.Timer myTimer = new System.Timers.Timer();
myTimer.Interval = TimerInterVal;
myTimer.AutoReset = true;
myTimer.Elapsed += new System.Timers.ElapsedEventHandler(notify_Elapsed);
myTimer.Enabled = true;
}
void notify_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
Liberary.WriteLog(DateTime.Now + " : Elapse");
}
protected override void OnStop()
{
Liberary.WriteLog(DateTime.Now + " : Stop");
}
}
In Service1.cs, We have created a job which will run after every 30 seconds and logged it in a file. Now rebuild project in debugging mode. It will create a exe file in bin > debug folder.
Install window service
To test your window service, we need to install it.
Go to Window > All program > Visual studio 2013 > Visual studio tools > Developer command prompt for VS2013
Now write following command :
InstallUtil.exe "../pathofproject/bin/debud/application.exe"
It will install the service. But remember it will run manually as setup in installer file start-type property. Now open the services app in all program. Here you will find your service and you can start it manually.
To debug window service without installing
- Remove program.cs file which was added by default when you created the project.
- Add following in Service1.cs file.
public static void Main()
{
#if DEBUG
Scheduler ser = new Scheduler();
ser.OnStart(null);
#else
ServiceBase.Run(new Scheduler());
#endif
}
No comments:
Post a Comment