Colin Brown
Colin Brown

Reputation: 599

Windows Service to run batch file, InstallUtil /i prevents service from starting?

I have a simple Windows service that calls a batch file to setup some processes on startup. Most of the batch file is fired correctly but InstallUtil /i fails to run as the Windows Service fails to start. (InstallUtil /u beforehand works though which I find strange) Here's some code for the windows service and the batch file:

namespace RecipopStartupService
{
public partial class Service1 : ServiceBase
{
    public Service1()
    {
        InitializeComponent();
    }

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


    public void ProcessBatchFile()
    {
        Process process = new Process();


        process.StartInfo.WorkingDirectory = "C:\\Webs\\AWS\\";
        process.StartInfo.FileName = "C:\\Webs\\AWS\\setup.bat";
        process.StartInfo.Arguments = "";
        process.StartInfo.Verb = "runas";
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

        process.StartInfo.CreateNoWindow = true;

        process.StartInfo.RedirectStandardOutput = false;

        process.Start();


        System.IO.StreamReader myOutput = process.StandardOutput;
        process.WaitForExit(200000);
        if (process.HasExited)
        {
            string results = myOutput.ReadToEnd();
        }


    }


    protected override void OnStop()
    {
    }
}
}

The batch file:

"C:\Program Files (x86)\Subversion\bin\SVN.exe" cleanup "C:\Webs\AWS\webs"
"C:\Program Files (x86)\Subversion\bin\SVN.exe" cleanup "C:\Webs\AWS\apps"

"C:\Program Files (x86)\Subversion\bin\SVN.exe" update "C:\Webs\AWS\webs"

REM The following directory is for .NET 2.0
set DOTNETFX2=%SystemRoot%\Microsoft.NET\Framework\v4.0.30319
set PATH=%PATH%;%DOTNETFX2%

echo Uninstalling MyService...
echo ---------------------------------------------------
InstallUtil /u "C:\Webs\AWS\apps\MyService.exe"
echo ---------------------------------------------------
echo Done.

"C:\Program Files (x86)\Subversion\bin\SVN.exe" update "C:\Webs\AWS\apps"

REM The following directory is for .NET 2.0
set DOTNETFX2=%SystemRoot%\Microsoft.NET\Framework\v4.0.30319
set PATH=%PATH%;%DOTNETFX2%

echo Installing MyService...
echo ---------------------------------------------------
InstallUtil /i "C:\Webs\AWS\apps\MyService.exe"
echo ---------------------------------------------------
echo Done.

NET START MyService

I've commented out various parts to determine what stops the service from starting. It's the InstallUtil /i section as I said previously.

If someone could advise that'd be great.

Thanks, Colin

Upvotes: 0

Views: 5615

Answers (1)

HTTP 410
HTTP 410

Reputation: 17618

I would debug your Windows service directly within Visual Studio rather than using a separate console app. Please see my blog entry here for details on doing this.

You can also install your service without using InstallUtil if you wish. Just set a reference to the System.Configuration.Install dll and use ManagedInstallerClass.InstallHelper.

Combining both approaches looks like this in C#:

// This is the entry point
static void Main(string[] args)
{
    // If parameter passed, act on it
    if ( args.Length > 0 )
    {
        switch (args[0] )
        {
            // Debug the service as a normal app from within Visual Studio
            case DEBUG:
                MyService DebugService = new MyService();
                DebugService.OnStart(null);
                break;
            // Install the service programatically
            case INSTALL:
                ManagedInstallerClass.InstallHelper(new string[] _
                { Assembly.GetExecutingAssembly().Location });
                break;
            // Un-install the service programatically
            case UNINSTALL:
                ManagedInstallerClass.InstallHelper(new string[] +
                { UNINSTALL, Assembly.GetExecutingAssembly().Location });
                break;
            // We don't understand this parameter!
            default:
                message = string.Concat(DEBUG, " to run service manually.", Environment.NewLine);
                message += string.Concat(INSTALL, " to install service.", Environment.NewLine);
                message += string.Concat(UNINSTALL, " to un-install service.", Environment.NewLine);
                message += string.Concat("Do not understand the command-line parameter ", args[0]);
                throw new System.NotImplementedException(message);
        }
    }
    // If no parameter passed, just start the service normally
    else
    {
        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[] { new MyService() };
        ServiceBase.Run(ServicesToRun);
    }
}

Upvotes: 2

Related Questions