2013-03-06 75 views
-1

當我嘗試安裝我的C#窗口服務,這是我所得到的,我在贏服務應用程序的新手,請註明,如果你需要進一步的信息,多appritiated:錯誤安裝C#Windows服務

Installing assembly 'C:\BatchFileManagerWinService\PCSBatchFileManagerWinService.exe'. 
Affected parameters are: 
    logtoconsole = 
    assemblypath = C:\BatchFileManagerWinService\PCSBatchFileManagerWinService.exe 
    logfile = C:\BatchFileManagerWinService\PCSBatchFileManagerWinService.InstallLog 
Installing service PCS Batch File Manager... 
Service PCS Batch File Manager has been successfully installed. 
Creating EventLog source PCS Batch File Manager in log Application... 
An exception occurred in the OnAfterInstall event handler of PCSBatchFileManagerWinService.ProjectInstaller. 
System.InvalidOperationException: Cannot start service PCS Batch File Manager on computer '.'. 
The inner exception System.ComponentModel.Win32Exception was thrown with the following error message: 
The service did not respond to the start or control request in a timely fashion. 
Rolling back assembly 'C:\BatchFileManagerWinService\PCSBatchFileManagerWinService.exe'. 
Affected parameters are: 
    logtoconsole = 
    assemblypath = C:\BatchFileManagerWinService\PCSBatchFileManagerWinService.exe 
    logfile = C:\BatchFileManagerWinService\PCSBatchFileManagerWinService.InstallLog 
Restoring event log to previous state for source PCS Batch File Manager. 
Service PCS Batch File Manager is being removed from the system... 
Service PCS Batch File Manager was successfully removed from the system. 

的ProjectInstaller代碼如下:

[RunInstaller(true)] 
public partial class ProjectInstaller : System.Configuration.Install.Installer 
{ 
    public ProjectInstaller() 
    { 
     InitializeComponent(); 

     //Read domain/Username and password 
     XmlDocument doc = new XmlDocument(); 
     doc.Load(System.Reflection.Assembly.GetExecutingAssembly().Location + ".config"); 
     XmlElement appSettings = (XmlElement)doc.DocumentElement.GetElementsByTagName("appSettings")[0]; 
     string username = null; 
     string password = null; 
     foreach (XmlElement setting in appSettings.GetElementsByTagName("add")) 
     { 
      string key = setting.GetAttribute("key"); 
      if (key == "WinServInstallUserName") username = setting.GetAttribute("value"); 
      if (key == "WinServInstallPassword") password = setting.GetAttribute("value"); 
     } 
     serviceProcessInstaller1.Account = ServiceAccount.User; 
     serviceProcessInstaller1.Username = username; 
     serviceProcessInstaller1.Password = password; 

     // Start Service 
     this.AfterInstall += new InstallEventHandler(ProjectInstaller_AfterInstall); 
    } 

    void ProjectInstaller_AfterInstall(object sender, InstallEventArgs e) 
    { 
     ServiceController sc = new ServiceController("PCS Batch File Manager"); 
     sc.Start(); 
    } 
} 
+1

Windows服務的啓動時間有限(不記得該服務是否可配置)。服務中是否有任何初始化可能會異常長或掛起? – Tim 2013-03-06 14:18:28

+0

發佈您的服務的代碼,它是無法完成OnStart – istepaniuk 2013-03-06 14:19:43

回答

1

在服務項目的Program.cs文件,添加

Debugger.Launch(); 

private static void Main(string[] args) 

第一線當您啓動服務時,它會問你你想怎麼調試,挑選您打開Visual Studio的參考。然後,您可以更好地瞭解導致問題的原因。

另外,當你完成時,不要忘記把這句話拿出來!

+0

感謝您的幫助!調試。罪魁禍首是log4net阻止服務啓動。我在AssemblyInfo.cs中評論了代碼// [assembly:log4net.Config.XmlConfigurator(ConfigFile =「log4net.config」,Watch = true)]並且服務正在啓動。我將刪除log4net代碼並編寫自定義錯誤日誌代碼。 – Gaurav 2013-03-06 16:48:26