Creating a Windows Service

They stupidly took away the Windows Service template in Visual Studio 2013 Express, but you can still create a Windows service. Here's how.

Start with a new Windows Application project and create an APIService.cs:

using System.Diagnostics;
using System.ServiceProcess;

namespace WindowsService
{
    public class APIService : ServiceBase
    {
        internal EventLog Log { get{ return this.EventLog; }}

        public APIService()
        {
            this.ServiceName = "API Service";
            this.CanStop = true;
            this.CanPauseAndContinue = false;
            this.AutoLog = true;
        }

        protected override void OnStart(string[] args)
        {
            base.OnStart(args);
            Log.WriteEntry("Starting APIService");
        }

        protected override void OnStop()
        {
            base.OnStop();
            Log.WriteEntry("Stopping APIService");
        }
    }
}

Create a Service.cs:

using System.ServiceProcess;

namespace WindowsService
{
    static class Service
    {
        /// 
        /// Entry point for Windows service.  Note the output type must be Windows Application or it won't start.
        /// 
        static void Main() { 
            ServiceBase.Run(new APIService());
        }
    }
}

Create a ProjectInstaller.cs class:

using System.ComponentModel;
using System.Configuration.Install;
using System.ServiceProcess;

namespace WindowsService
{
    [RunInstaller(true)]
    public class ProjectInstaller : Installer
    {
        public ProjectInstaller()
        {
            var processInstaller = new ServiceProcessInstaller();
            var installer = new ServiceInstaller();

            processInstaller.Account = ServiceAccount.NetworkService; //change this to app ID
            processInstaller.Username = null;
            processInstaller.Password = null;
            
            installer.DisplayName = "APIService";
            installer.Description = "API service for serving JSON";
            installer.StartType = ServiceStartMode.Automatic;
            installer.ServiceName = "APIService";

            this.Installers.Add(installer);
            this.Installers.Add(processInstaller);

        }

        //private void serviceInstaller1_AfterInstall(object sender, InstallEventArgs e)
        //{ }

        //private void serviceProcessInstaller1_AfterInstall(object sender, InstallEventArgs e)
        //{ }
    }
}

To install, use the installutil.exe tool. It would simply be "installutil path-to-windows-service.exe".