How
to create Windows Service in Visual studio
How
to create Windows Service in Visual studio 2005 (.Net)
This is a simple example for windows
service which automatically send a good morning mail on every day in morning.This
service will run for every one hour, if hour value equals to 9 then it sends the
mail to the recipants.
First Create a project....
File --> New --> Project
Then select Windows in Project Types (Left side panel). In right side Templates
panel select WindowsService.
Provide a name for windows service.
This will look like this...
Then The project will creates a Service with Service1.cs[design]
as default. Rename it with proper name.
In that service Click on the link
click here to switch to code view
It open code window for service
Service1.cs.
Here write the following code.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Timers;
using System.Net.NetworkInformation;
using System.Net.Security;
using System.Net.Mail;
namespace CHGoodMorningService
{
public partial
class CHGoodMorningService
: ServiceBase
{
public CHGoodMorningService()
{
InitializeComponent();
}
Timer CHTime=new
Timer();
protected override void OnStart(string[] args)
{
CHTime.Elapsed += new
ElapsedEventHandler(OnElapsedTime);
CHTime.Interval = 3600000;
//One Hour- The windows service
will run automatically for every hour
CHTime.Enabled =
true;
}
private void
OnElapsedTime(object source,
ElapsedEventArgs e)
{
MailMessage objMail
= new MailMessage();
objMail.From=new MailAddress("Sender-MailID","Sender Name");
objMail.To.Add("Recipent MailID");
objMail.Subject="The Legend
says Good Morning to You.";
objMail.IsBodyHtml=true;
objMail.Body = "Hi, Good Morning";
if(DateTime.Now.Hour==9)
{
Send(objMail);
}
}
public static
void Send(MailMessage
msg)
{
SmtpClient SmtpMail
= new SmtpClient();
SmtpMail.Host = "ServerName";
SmtpMail.Credentials=new
System.Net.NetworkCredential("mail","password");
SmtpMail.Send(msg);
}
protected override
void OnStop()
{
CHTime.Enabled=false;
}
}
}
Creating a windows service is over
now you have to create a set up project to install windows service.
Use a compiled Setup
project to install the Windows Service
After you complete the steps in
the "Create a Windows Service project" section to configure the Windows Service
project, you can add a deployment project that packages the service application
so that the service application can be installed. To do this, follow these steps:
- Add a new project to your CHGoodMorningService
project.
- In Solution Explorer,
right-click Solution 'CHGoodMorningService', point to Add, and then click New Project.
- Under Project Types,
click Setup and Deployment Projects or Setup and Deployment.
- Under Templates, click
Setup Project.
- In the Name box, type
CHGoodMorningServiceSetup.
- In the Location box,
type C:\, and then click OK.
- Tell the deployment project
what the deployment project will package.
- In Solution Explorer,
right-click CHGoodMorningServiceSetup, point to Add, and then click Project Output.
- In the Add Project
Output Group dialog box, click CHGoodMorningService in the Project box.
- Click Primary Output,
and then click OK.
- For correct installation, you
have to add only primary output. To add the custom actions, follow these steps:
- In Solution Explorer,
right-click CHGoodMorningServiceSetup, point to View, and then click Custom Actions.
- Right-click Custom
Actions, and then click Add Custom Action.
- Click Application Folder,
and then click OK.
- Click Primary output
from CHGoodMorningService (Active), and then click OK. Notice that Primary output
appears under Install, Commit, Rollback and Uninstall.
- By default, Setup projects are
not included in the build configuration. To build the solution, follow these steps:
- Use one of the following
methods:
- Right-click CHGoodMorningService,
and then click Build. Then, right-click CHGoodMorningServiceSetup, and then click
Build.
- To build the whole solution
at the same time, click Configuration Manager on the Build menu, and then click
to select the Build check box for CHGoodMorningServiceSetup.
- Press CTRL+SHIFT+B
to build the whole solution. When the solution is built, you have a complete Setup
package for the service.
- To install the service, right-click
CHGoodMorningServiceSetup, and then click Install.
- In the CHGoodMorningServiceSetup
dialog box, click Next three times. Notice that a progress bar appears while the
Setup program is installing the service.
- When the service is installed,
click Close.