Friday, 14 October 2011

step 7 to 9

Step 7: Add the 'WindowsServiceHostedService.dll' as reference to the project. This assembly will going to act as service.
Step 8: OnStart method of the service, we can write the hosting code for WCF. We have to make sure that we are using only one service host object. On stop method you need to close the Service Host. Following code show how to host WCF service in Windows service.
WCFHostedWindowsService.cs
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;
using System.ServiceModel.Description;

namespace WCFHostedWindowsService
{
    partial class WCFHostedWindowsService : ServiceBase
    {
        ServiceHost m_Host;
        
        public WCFHostedWindowsService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            if (m_Host != null)
            {
                m_Host.Close();
            }
            //Create a URI to serve as the base address
            Uri httpUrl = new Uri("http://localhost:8090/MyService/SimpleCalculator");
            //Create ServiceHost
            m_Host = new ServiceHost
            (typeof(WindowsServiceHostedService.SimpleCalculator), httpUrl);
            //Add a service endpoint
            m_Host.AddServiceEndpoint
            (typeof(WindowsServiceHostedService.ISimpleCalculator), new WSHttpBinding(), "");
            //Enable metadata exchange
            ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
            smb.HttpGetEnabled = true;
            m_Host.Description.Behaviors.Add(smb);
            //Start the Service
            m_Host.Open();


        }

        protected override void OnStop()
        {
            if (m_Host != null)
            {
                m_Host.Close();
                m_Host = null;
            }
        }
        static void Main()
        {
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[] 
   { 
    new WCFHostedWindowsService() 
   };
            ServiceBase.Run(ServicesToRun);

        }
    }
}
Step 9: In order to install the service we need to have the Installer class for the Windows service. So add new Installer class to the project, which is inherited from the Installer class. Please find the below code for mentioning the Service name, StartUp type etc of the service.
ServiceInstaller.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceProcess;
using System.Configuration.Install;
using System.ComponentModel;
using System.Configuration;


namespace WCFHostedWindowsService
{
    [RunInstaller(true)]
    public class WinServiceInstaller : Installer
    {
        private ServiceProcessInstaller process;
        private ServiceInstaller service;

        public WinServiceInstaller()
        {
            process = new ServiceProcessInstaller();
            process.Account = ServiceAccount.NetworkService;
            service = new ServiceInstaller();
            service.ServiceName = "WCFHostedWindowsService";
            service.DisplayName = "WCFHostedWindowsService";
            service.Description = "WCF Service Hosted";
            service.StartType = ServiceStartMode.Automatic;
            Installers.Add(process);
            Installers.Add(service);
        }
    }
}

No comments:

Post a Comment