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...
WindowsService
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:

  1. Add a new project to your CHGoodMorningService project.
    1. In Solution Explorer, right-click Solution 'CHGoodMorningService', point to Add, and then click New Project.
    2. Under Project Types, click Setup and Deployment Projects or Setup and Deployment.
    3. Under Templates, click Setup Project.
    4. In the Name box, type CHGoodMorningServiceSetup.
    5. In the Location box, type C:\, and then click OK.

      WindowsServiceSetUp
  2. Tell the deployment project what the deployment project will package.
    1. In Solution Explorer, right-click CHGoodMorningServiceSetup, point to Add, and then click Project Output.
    2. In the Add Project Output Group dialog box, click CHGoodMorningService in the Project box.
    3. Click Primary Output, and then click OK.

  3. For correct installation, you have to add only primary output. To add the custom actions, follow these steps:
    1. In Solution Explorer, right-click CHGoodMorningServiceSetup, point to View, and then click Custom Actions.
    2. Right-click Custom Actions, and then click Add Custom Action.
    3. Click Application Folder, and then click OK.
    4. Click Primary output from CHGoodMorningService (Active), and then click OK. Notice that Primary output appears under Install, Commit, Rollback and Uninstall.

  4. By default, Setup projects are not included in the build configuration. To build the solution, follow these steps:
    1. 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.
    2. Press CTRL+SHIFT+B to build the whole solution. When the solution is built, you have a complete Setup package for the service.

  5. To install the service, right-click CHGoodMorningServiceSetup, and then click Install.
  6. In the CHGoodMorningServiceSetup dialog box, click Next three times. Notice that a progress bar appears while the Setup program is installing the service.
  7. When the service is installed, click Close.