How To Make Window Service in .Net
9:36 PM Posted In Window Sevice Edit This 0 Comments »
Open Visual Studio.net > Select New Project
2.Select Windows Service and give Name as 'GoodDay' and Click Ok.
3.Right Click the Design and Select Properties and give the Name and ServiceName as
'GoodDay' and set AutoLog to 'True'
4.Drag and drop the OleDbConnection and Select its properties and change its name to
'conn' and provide the connection String(I provided the Database sample(Ms
Access)) along with this code.
5.Right Click the Design and select View Code and
Select Project > Add Reference > Select System.Web >Press Ok
6.Now add the following to your code
using System.Web.Mail;
using System.Data.OleDb;
7.Declare DateTime mdt=DateTime.Now;
8. In static void Main()
Edit the following Code
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new GoodDay() };
9. Edit the following code in onstart()
DataSet ds = new DataSet();
protected override void OnStart(string[] args)
{
DateTime dt=DateTime.Now;
conn.Open();
OleDbDataAdapter da=new OleDbDataAdapter("select * from Empdata",conn);
da.Fill(ds);
if(dt.ToShortDateString()==mdt.ToShortDateString())
{
foreach(DataRow dr in ds.Tables[0].Rows)
{
mdt=DateTime.Now.AddDays(+1);
String mailtxt="";
MailMessage mm = new MailMessage();
mm.BodyFormat = MailFormat.Html;
mm.To = dr["emp_email"].ToString();
mm.From = "xyz@xyz.com";
mm.Subject="Good Day";
mailtxt = ""+"Hi "+dr["emp_name"].ToString()+"," + "
";
mailtxt=mailtxt+""+"Good Day." + "
";
mailtxt=mailtxt+""+"May today be filled with sunshine and smile, laughter and love." + "
";
mailtxt=mailtxt+"Cheers!" + "
";
mm.Body = mailtxt;
SmtpMail.SmtpServer="localhost";
SmtpMail.Send(mm);
}
}
}
10.Go to Design,right click and Select Add Installer ,you will notice two controls
serviceProcessInstaller1,serviceInstaller1
11.Select serviceProcessInstaller1 and go to its properties and change the Account
type to 'Local System'
12. Select serviceInstaller1 and go to its properties and change DisplayName and
ServiceName to 'GoodDay' and make the StartType to 'Automatic'
13.Build the Solution(Cntrl+Shift+B)
14.Open the Visual Studio .Net Command Prompt and give the path of application
for example C:\bin\Debug and then type InstallUtil GoodDay.exe
to uninstall, InstallUtil /u GoodDay.exe
15. After Installation ,right click My Computer and
Select 'Manage'
and Select 'Service and Applications'
and in that Select 'Services'
Select 'GoodDay' and start the Service.
2.Select Windows Service and give Name as 'GoodDay' and Click Ok.
3.Right Click the Design and Select Properties and give the Name and ServiceName as
'GoodDay' and set AutoLog to 'True'
4.Drag and drop the OleDbConnection and Select its properties and change its name to
'conn' and provide the connection String(I provided the Database sample(Ms
Access)) along with this code.
5.Right Click the Design and select View Code and
Select Project > Add Reference > Select System.Web >Press Ok
6.Now add the following to your code
using System.Web.Mail;
using System.Data.OleDb;
7.Declare DateTime mdt=DateTime.Now;
8. In static void Main()
Edit the following Code
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new GoodDay() };
9. Edit the following code in onstart()
DataSet ds = new DataSet();
protected override void OnStart(string[] args)
{
DateTime dt=DateTime.Now;
conn.Open();
OleDbDataAdapter da=new OleDbDataAdapter("select * from Empdata",conn);
da.Fill(ds);
if(dt.ToShortDateString()==mdt.ToShortDateString())
{
foreach(DataRow dr in ds.Tables[0].Rows)
{
mdt=DateTime.Now.AddDays(+1);
String mailtxt="";
MailMessage mm = new MailMessage();
mm.BodyFormat = MailFormat.Html;
mm.To = dr["emp_email"].ToString();
mm.From = "xyz@xyz.com";
mm.Subject="Good Day";
mailtxt = ""+"Hi "+dr["emp_name"].ToString()+"," + "
";
mailtxt=mailtxt+""+"Good Day." + "
";
mailtxt=mailtxt+""+"May today be filled with sunshine and smile, laughter and love." + "
";
mailtxt=mailtxt+"Cheers!" + "
";
mm.Body = mailtxt;
SmtpMail.SmtpServer="localhost";
SmtpMail.Send(mm);
}
}
}
10.Go to Design,right click and Select Add Installer ,you will notice two controls
serviceProcessInstaller1,serviceInstaller1
11.Select serviceProcessInstaller1 and go to its properties and change the Account
type to 'Local System'
12. Select serviceInstaller1 and go to its properties and change DisplayName and
ServiceName to 'GoodDay' and make the StartType to 'Automatic'
13.Build the Solution(Cntrl+Shift+B)
14.Open the Visual Studio .Net Command Prompt and give the path of application
for example C:\bin\Debug and then type InstallUtil GoodDay.exe
to uninstall, InstallUtil /u GoodDay.exe
15. After Installation ,right click My Computer and
Select 'Manage'
and Select 'Service and Applications'
and in that Select 'Services'
Select 'GoodDay' and start the Service.
0 comments:
Post a Comment