Windows服务运行批处理文件,InstallUtil/i阻止服务启动



我有一个简单的Windows服务,它在启动时调用批处理文件来设置一些进程。大部分批处理文件已正确启动,但由于Windows服务无法启动,InstallUtil/i无法运行。(尽管InstallUtil/u事先有效,但我觉得很奇怪)以下是windows服务和批处理文件的一些代码:

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()
    {
    }
}
}

批处理文件:

"C:Program Files (x86)SubversionbinSVN.exe" cleanup "C:WebsAWSwebs"
"C:Program Files (x86)SubversionbinSVN.exe" cleanup "C:WebsAWSapps"
"C:Program Files (x86)SubversionbinSVN.exe" update "C:WebsAWSwebs"
REM The following directory is for .NET 2.0
set DOTNETFX2=%SystemRoot%Microsoft.NETFrameworkv4.0.30319
set PATH=%PATH%;%DOTNETFX2%
echo Uninstalling MyService...
echo ---------------------------------------------------
InstallUtil /u "C:WebsAWSappsMyService.exe"
echo ---------------------------------------------------
echo Done.
"C:Program Files (x86)SubversionbinSVN.exe" update "C:WebsAWSapps"
REM The following directory is for .NET 2.0
set DOTNETFX2=%SystemRoot%Microsoft.NETFrameworkv4.0.30319
set PATH=%PATH%;%DOTNETFX2%
echo Installing MyService...
echo ---------------------------------------------------
InstallUtil /i "C:WebsAWSappsMyService.exe"
echo ---------------------------------------------------
echo Done.
NET START MyService

我已经对各个部分进行了注释,以确定是什么阻止了服务的启动。正如我之前所说,这是InstallUtil/i部分。

如果有人能提供建议,那就太好了。

谢谢,Colin

我会直接在Visual Studio中调试您的Windows服务,而不是使用单独的控制台应用程序。请查看我的博客条目,了解有关此操作的详细信息。

如果您愿意,也可以在不使用InstallUtil的情况下安装您的服务。只需设置对System.Configuration.Install dll的引用,然后使用ManagedInstallerClass.InstallHelper.

在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);
    }
}

最新更新