Search This Blog

Thursday, November 20, 2014

Creating and installing Windows Service in a development enviroment.

I hadn't worked on a Windows Service in 4 years so I forgot some of the fun necessary to create a new Windows Service.

Useful References
SC.exe - http://technet.microsoft.com/en-us/library/cc754599.aspx

Using SC.exe 

Settings Description for Service:


Steps to create a new Windows service
(Using Windows 7 Pro and Visual Studio 2013 Ultimate)

  • Add new project to solution
  • Select C# Windows, Windows Service template
  • Name project SoftwareUpdaterService and create.
      Note: VS 2013 template creates the necessary service base code to be a functional service:

     public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
        }

        protected override void OnStop()
        {
        }

   

  •  Build project and confirmed ..\SoftwareUpdaterService\bin\Debug\ SoftwareUpdaterService.exe
  • (I forgot this step) use SC to install the service
  • Open VS 2013 command prompt, switch to  bin\debug folder
  • Run this command:
SC create MergeSoftwareUpdater displayname= "Merge Healthcare Software Updater Service" binpath= "C:\CAP30\Tools\Software Updater Service\SoftwareUpdater\SoftwareUpdaterService\bin\Debug\SoftwareUpdaterService.exe" start= auto


  Creates registry entry for service:
HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services

MergeSoftwareUpdater <= service key.


  • Open Services manager and service should appear.
  • Added Installer class to service project (lifted it from an existing project)
  • Now InstallUtil.exe will install the project as a service.
     InstallUtil.exe SoftwareUpdaterService.exe
(Added) using System.Net;
              using System.ServiceProcess;
        private void InitializeComponent()
        {
            this.serviceProcessInstaller = new System.ServiceProcess.ServiceProcessInstaller();
            this.serviceInstaller = new System.ServiceProcess.ServiceInstaller();
            // 
            // deliveryServiceProcessInstaller
            // 
            this.serviceProcessInstaller.Password = null;
            this.serviceProcessInstaller.Username = null;
            // 
            // deliveryServiceInstaller
            // 
            this.serviceInstaller.ServiceName = "Merge Healthcare Software Updater Service";
            this.serviceInstaller.Description = "Allows software updates to be scheduled and run automatically.";
            this.serviceInstaller.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
            // 
            // ProjectInstaller
            // 
            this.Installers.AddRange(new System.Configuration.Install.Installer[] {
                                                                                      this.serviceProcessInstaller,
                                                                                      this.serviceInstaller});
        }

        public override void Install(IDictionary stateSaver) 
        {                    
            if (Context.Parameters["user"] != null && Context.Parameters["user"].Length != 0 && Context.Parameters["password"].Length != 0 && Context.Parameters["password"] != null) 
            {
                string user = Context.Parameters["user"];
                // if the user starts with . then we want to replace that with the local machine's actual hostname
                if (user.StartsWith(".")) 
                {
                    user = Dns.GetHostName() + user.Substring(1, user.Length - 1);
                }
                serviceProcessInstaller.Username = user;
                serviceProcessInstaller.Password = Context.Parameters["password"];
                serviceProcessInstaller.Account = ServiceAccount.User;
            } 
            else 
            {
                serviceProcessInstaller.Account = ServiceAccount.LocalSystem;
            }
            
            base.Install(stateSaver);
        }




No comments: