2012-07-15 115 views
0

我有一个简单的Windows服务,调用批处理文件来设置启动时的一些进程。大多数批处理文件被正确激活,但由于Windows服务无法启动,InstallUtil/i无法运行。 (InstallUtil/U事前工作,虽然我觉得奇怪)下面是Windows服务和批处理文件中的一些代码:Windows服务来运行批处理文件,InstallUtil /我阻止服务启动?

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)\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 

我注释掉各个部分确定什么使服务停止启动。正如我之前所说的,它是InstallUtil/i部分。

如果有人可以建议,那会很好。

感谢, 科林

+0

这是所有的服务代码? – adrianbanks 2012-07-15 16:41:08

+0

该代码不适用于服务,这是一个控制台应用程序。你确定你正在建立一个* real *服务吗?它应该有一个'OnStart()'方法,至少不在那里。 – 2012-07-17 09:29:53

+0

对不起,你是对的 - 我创建了一个控制台应用程序,该应用程序触发相同的方法来检查是否工作。我已经编辑了上面的代码,但被解雇的方法是一样的。谢谢 – 2012-07-17 17:36:09

回答

2

我会直接在Visual Studio中调试您的Windows服务,而不是使用一个单独的控制台应用程序。有关详细信息,请参阅我的blog entry here

如果您愿意,您也可以在不使用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); 
    } 
} 
相关问题