How to host service as a Windows Services

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

Code Snippet
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.ServiceModel;
  6. namespace CalcService
  7. {
  8.    [ServiceContract]
  9.     public interface ICalc
  10.     {
  11.        [OperationContract]
  12.        int GetSum(int i, int j);
  13.     }
  14.    public class CalcServices : ICalc
  15.    {
  16.        #region ICalc Members
  17.        public int GetSum(int i, int j)
  18.        {
  19.            return i + j;
  20.        }
  21.        #endregion
  22.    }
  23. }

 

Following is the App.config.

 

Code Snippet
  1. <?xml version=1.0 encoding=utf-8 ?>
  2. <configuration>
  3.   <system.serviceModel>
  4.     <services>
  5.       <service name=CalcService.CalcServices>
  6.         <endpoint address=“” binding=basicHttpBinding contract=CalcService.ICalc/>
  7.         <host>
  8.           <baseAddresses>
  9.             <add baseAddress=http://localhost:1234/CalcServices/>
  10.           </baseAddresses>
  11.         </host>
  12.         
  13.         
  14.       </service>
  15.       
  16.     </services>
  17.     
  18.     
  19.     
  20.   </system.serviceModel>  
  21. </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..

Code Snippet
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Diagnostics;
  6. using System.Linq;
  7. using System.ServiceProcess;
  8. using System.Text;
  9. using System.ServiceModel;
  10. namespace CalcService
  11. {
  12.     partial class Service1 : ServiceBase
  13.     {
  14.         public Service1()
  15.         {
  16.             InitializeComponent();
  17.         }
  18.         ServiceHost sn;
  19.         protected override void OnStart(string[] args)
  20.         {
  21.             sn = new ServiceHost(typeof(CalcService.CalcServices), new Uri[] { });
  22.             sn.Open();
  23.         }
  24.         protected override void OnStop()
  25.         {
  26.             sn.Close();
  27.         }
  28.     }
  29. }

Write down following code into Installer1

Code Snippet
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Configuration.Install;
  6. using System.Linq;
  7. using System.ServiceProcess;
  8. namespace CalcService
  9. {
  10.     [RunInstaller(true)]
  11.     public partial class Installer1 : Installer
  12.     {
  13.         public Installer1()
  14.         {
  15.             InitializeComponent();
  16.             ServiceProcessInstaller processInstaller = new ServiceProcessInstaller();
  17.             ServiceInstaller serviceInstaller = new ServiceInstaller();
  18.             processInstaller.Account = ServiceAccount.LocalSystem;
  19.             serviceInstaller.DisplayName = “Service1″;
  20.             serviceInstaller.Description = “CalcService.”;
  21.             serviceInstaller.ServiceName = “Service1″;
  22.             serviceInstaller.StartType = ServiceStartMode.Manual;
  23.             Installers.Add(processInstaller);
  24.             Installers.Add(serviceInstaller);
  25.         }
  26.     }
  27. }

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

 

Code Snippet
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.ServiceProcess;
  6. namespace CalcService
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             ServiceBase[] ServicesToRun;
  13.             ServicesToRun = new ServiceBase[] { new Service1() };
  14.             ServiceBase.Run(ServicesToRun);
  15.         }
  16.     }
  17. }

 

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 …:)

Advertisement

About miteshisheth
Believe in sharing knowledge.

One Response to How to host service as a Windows Services

  1. Joan says:

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.