2012-07-15 113 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); 
    } 
} 
相關問題