public string GetWindowsServiceFolderPath( WindowsService service, bool useDebugFolderIfDevelopmentInstallation )
 {
     var path = EwlStatics.CombinePaths( generalInstallationLogic.Path, service.Name );
     if( runtimeConfiguration.InstallationType == InstallationType.Development )
         path = EwlStatics.CombinePaths( path, EwlStatics.GetProjectOutputFolderPath( useDebugFolderIfDevelopmentInstallation ) );
     return path;
 }
        private void generateWindowsServiceCode( DevelopmentInstallation installation, WindowsService service )
        {
            var serviceProjectGeneratedCodeFolderPath = EwlStatics.CombinePaths( installation.GeneralLogic.Path, service.Name, "Generated Code" );
            Directory.CreateDirectory( serviceProjectGeneratedCodeFolderPath );
            var isuFilePath = EwlStatics.CombinePaths( serviceProjectGeneratedCodeFolderPath, "ISU.cs" );
            IoMethods.DeleteFile( isuFilePath );
            using( TextWriter writer = new StreamWriter( isuFilePath ) ) {
                writer.WriteLine( "using System;" );
                writer.WriteLine( "using System.ComponentModel;" );
                writer.WriteLine( "using System.Reflection;" );
                writer.WriteLine( "using System.Runtime.InteropServices;" );
                writer.WriteLine( "using System.ServiceProcess;" );
                writer.WriteLine( "using System.Threading;" );
                writer.WriteLine( "using EnterpriseWebLibrary;" );
                writer.WriteLine( "using EnterpriseWebLibrary.DataAccess;" );
                writer.WriteLine( "using EnterpriseWebLibrary.WindowsServiceFramework;" );
                writer.WriteLine();
                writeAssemblyInfo( writer, installation, service.Name );
                writer.WriteLine();
                writer.WriteLine( "namespace " + service.NamespaceAndAssemblyName + " {" );

                writer.WriteLine( "internal static partial class Program {" );

                writer.WriteLine( "[ MTAThread ]" );
                writer.WriteLine( "private static void Main() {" );
                writer.WriteLine( "InitStatics();" );
                writer.WriteLine( "try {" );
                writer.WriteLine(
                    "TelemetryStatics.ExecuteBlockWithStandardExceptionHandling( () => ServiceBase.Run( new ServiceBaseAdapter( new " + service.Name.EnglishToPascal() +
                    "() ) ) );" );
                writer.WriteLine( "}" );
                writer.WriteLine( "finally {" );
                writer.WriteLine( "GlobalInitializationOps.CleanUpStatics();" );
                writer.WriteLine( "}" );
                writer.WriteLine( "}" );

                writer.WriteLine( "internal static void InitStatics() {" );
                writer.WriteLine( "SystemInitializer globalInitializer = null;" );
                writer.WriteLine( "initGlobalInitializer( ref globalInitializer );" );
                writer.WriteLine( "var dataAccessState = new ThreadLocal<DataAccessState>( () => new DataAccessState() );" );
                writer.WriteLine(
                    "GlobalInitializationOps.InitStatics( globalInitializer, \"" + service.Name +
                    "\" + \" Executable\", false, mainDataAccessStateGetter: () => dataAccessState.Value );" );
                writer.WriteLine( "}" );

                writer.WriteLine( "static partial void initGlobalInitializer( ref SystemInitializer globalInitializer );" );

                writer.WriteLine( "}" );

                writer.WriteLine( "[ RunInstaller( true ) ]" );
                writer.WriteLine( "public class Installer: System.Configuration.Install.Installer {" );

                writer.WriteLine( "public Installer() {" );
                writer.WriteLine( "Program.InitStatics();" );
                writer.WriteLine( "try {" );
                writer.WriteLine( "var code = GlobalInitializationOps.ExecuteAppWithStandardExceptionHandling( delegate {" );
                writer.WriteLine( "Installers.Add( WindowsServiceMethods.CreateServiceProcessInstaller() );" );
                writer.WriteLine( "Installers.Add( WindowsServiceMethods.CreateServiceInstaller( new " + service.Name.EnglishToPascal() + "() ) );" );
                writer.WriteLine( "} );" );
                writer.WriteLine( "if( code != 0 )" );
                writer.WriteLine(
                    "throw new ApplicationException( \"Service installer objects could not be created. More information should be available in a separate error email from the service executable.\" );" );
                writer.WriteLine( "}" );
                writer.WriteLine( "finally {" );
                writer.WriteLine( "GlobalInitializationOps.CleanUpStatics();" );
                writer.WriteLine( "}" );
                writer.WriteLine( "}" );

                writer.WriteLine( "}" );

                writer.WriteLine( "internal partial class " + service.Name.EnglishToPascal() + ": WindowsServiceBase {" );
                writer.WriteLine( "internal " + service.Name.EnglishToPascal() + "() {}" );
                writer.WriteLine( "string WindowsServiceBase.Name { get { return \"" + service.Name + "\"; } }" );
                writer.WriteLine( "}" );

                writer.WriteLine( "}" );
            }
        }
 private void runInstallutil( WindowsService service, bool uninstall )
 {
     try {
         EwlStatics.RunProgram(
             EwlStatics.CombinePaths( RuntimeEnvironment.GetRuntimeDirectory(), "installutil" ),
             ( uninstall ? "/u " : "" ) + "\"" +
             EwlStatics.CombinePaths( GetWindowsServiceFolderPath( service, true ), service.NamespaceAndAssemblyName + ".exe" /* file extension is required */ ) + "\"",
             "",
             true );
     }
     catch( Exception e ) {
         const string message = "Installer tool failed.";
         if( e.Message.Contains( typeof( EmailSendingException ).Name ) )
             throw new UserCorrectableException( message, e );
         throw new ApplicationException( message, e );
     }
 }