Creating Windows Service and Packaging into Setup in C#

No.of Views1192
Bookmarked0 times
Downloads 
Votes0
By  amalhashim   On  14 May 2010 23:05:12
Tag : Windows Service , How to
Creating Windows Service and Packaging into Setup in C#
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

In this article, i have shared with you, create windows service with visual studio 2008 and then create setup file to deploy in production.The windows service deployment is easy but when we are create setup, we have to make sure some key points. i will give the all key points here with steps by step guide.

Create Service

Open Visual Studio and File->New Project 

Image Loading

Select Window Service Project from the template.Give the project name and location.Click “Ok” will create a project as shown in the following screen shot.

 

Image Loading

Open the Service1.cs by right clicking and selecting View Code 

Image Loading

OnStart - control the service startup

OnStart - control the service stoppage

Note:- Never put an infinite loop inside the OnStart method. If you are looking for something like Remoting or Socket programming that needs to keep listening; Start a thread on OnStart that will handle it.Here the point to be noted is that OnStart must return.

Here am going to demonstrate an Event Logging Service using Timer elapsed event

public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}
System.Timers.Timer myTimer;
string Source;
string Log;
string Event;
protected override void OnStart(string[] args)
{
Source = "Timer Service";
Log = "Application";
Event = "Timer Event";
myTimer = new System.Timers.Timer();
myTimer.Interval = 10000;
myTimer.Enabled = true;
myTimer.Elapsed += new System.Timers.ElapsedEventHandler(myTimer_Elapsed);
if (!EventLog.SourceExists(Source))
EventLog.CreateEventSource(Source, Log);
}
private void myTimer_Elapsed(object sender, EventArgs e)
{
if (EventLog.SourceExists(Source))
{
EventLog.WriteEntry(Source, Event);
EventLog.WriteEntry(Source, Event, EventLogEntryType.Warning, 1234);
}
}
protected override void OnStop()
{
}
}

Now right click Service1.cs and select View Designer.In the designer right click and select Add Installer.This will create two components 

Image Loading

Now select the components and change their properties accordingly.Using these components you can configure the authentication, name of service, service startup, etc.Select the property page of the Project and set the Startup Object.Compile the project.Window service is ready.

Creating a setup for deploying windows service

Next, I will explain how you can package the service into one installer that will automate the installation process.

From Visual Studio

File->Add->New Project 

Image Loading

From Other Project Types select Setup Project.Give name and click OK.It will create a project as shown in the following screen shot. 

Image Loading

Right click TimerServiceSetup Select Add->Project Output Which will brings the following window. 

Image Loading

From this window select TimerService from Project and Select Primary Output and click OK.Now right click and select View->Custom Actions.The custom action window will be visible in the designer area.Right Click Custom Actions and select Add Custom Actions.This will bring a new window. Double click the Application Folder and click Ok.

Build the project.Setup is ready.enjoy.

 
Sign Up to vote for this article
 
About Author
 
amalhashim
Occupation-Software Engineer
Company-Aditi Technologies
Member Type-Senior
Location-Not Provided
Joined date-07 Jun 2009
Home Page-http://lamahashim.blogspot.com
Blog Page-http://lamahashim.blogspot.com
I have done my masters in Computer Applications and graduation in Computer Science. I have great passion in working with Microsoft tool and technologies. I am also a Microsoft Most Valuable Professional. Personally my objective is to design/develop applications which eases user experience and performs better in long run.
 
 
Other popularSectionarticles
    Comments
    There is no comments for this articles.
    Leave a Reply
    Title:
    Display Name:
    Email:
    (not display in page for the security purphase)
    Website:
    Message:
    Please refresh your screen using Ctrl+F5
    If you can't read this number refresh your screen
    Please input the anti-spam code that you can read in the image.
    ^ Scroll to Top