How to host service as a Windows Services
December 14, 2009 1 Comment
We can host services three ways…
1. Self Host
2. IIS
3. Window Service
This article is for Hosing service as a Windows Services.
I have following services..
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.ServiceModel;
- namespace CalcService
- {
- [ServiceContract]
- public interface ICalc
- {
- [OperationContract]
- int GetSum(int i, int j);
- }
- public class CalcServices : ICalc
- {
- #region ICalc Members
- public int GetSum(int i, int j)
- {
- return i + j;
- }
- #endregion
- }
- }
Following is the App.config.
- <?xml version=“1.0“ encoding=“utf-8“ ?>
- <configuration>
- <system.serviceModel>
- <services>
- <service name=“CalcService.CalcServices“>
- <endpoint address=“” binding=“basicHttpBinding“ contract=“CalcService.ICalc“/>
- <host>
- <baseAddresses>
- <add baseAddress=“http://localhost:1234/CalcServices“/>
- </baseAddresses>
- </host>
- </service>
- </services>
- </system.serviceModel>
- </configuration>
Now Add two files in the solution.
1. Windows Service and
2. Installer Class
Write down following code into Service1 (Windows Service class) class..
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Diagnostics;
- using System.Linq;
- using System.ServiceProcess;
- using System.Text;
- using System.ServiceModel;
- namespace CalcService
- {
- partial class Service1 : ServiceBase
- {
- public Service1()
- {
- InitializeComponent();
- }
- ServiceHost sn;
- protected override void OnStart(string[] args)
- {
- sn = new ServiceHost(typeof(CalcService.CalcServices), new Uri[] { });
- sn.Open();
- }
- protected override void OnStop()
- {
- sn.Close();
- }
- }
- }
Write down following code into Installer1
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Configuration.Install;
- using System.Linq;
- using System.ServiceProcess;
- namespace CalcService
- {
- [RunInstaller(true)]
- public partial class Installer1 : Installer
- {
- public Installer1()
- {
- InitializeComponent();
- ServiceProcessInstaller processInstaller = new ServiceProcessInstaller();
- ServiceInstaller serviceInstaller = new ServiceInstaller();
- processInstaller.Account = ServiceAccount.LocalSystem;
- serviceInstaller.DisplayName = “Service1″;
- serviceInstaller.Description = “CalcService.”;
- serviceInstaller.ServiceName = “Service1″;
- serviceInstaller.StartType = ServiceStartMode.Manual;
- Installers.Add(processInstaller);
- Installers.Add(serviceInstaller);
- }
- }
- }
Now we have written both the service and Installer. Now we have to write code for start up means some ware I’ve to call ..
I’ve written in main program
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.ServiceProcess;
- namespace CalcService
- {
- class Program
- {
- static void Main(string[] args)
- {
- ServiceBase[] ServicesToRun;
- ServicesToRun = new ServiceBase[] { new Service1() };
- ServiceBase.Run(ServicesToRun);
- }
- }
- }
Now we have done everything. Final step is after compiling the solution, we have to install through command prompt.
c:> Installutil –i c:…..DebugCaleService.exe
Now if you check in service.msi, you can get the service.
Hope this is useful …:)



This is very good article. Can u please write one article for IIS hosting? It will be great for me.