简体   繁体   中英

Error when i try to install C# Windows Service

when i try to install my C# window service this is what i get, i am newbie in win service applications, please mention if you need further information, much appritiated:

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.

The ProjectInstaller code is as below:

  [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();
    }
}

In your service project's Program.cs file, add

Debugger.Launch();

as the first line in

private static void Main(string[] args)

When you start the service, it will ask you how you want to debug, pick the Visual Studio reference you have open. Then you can have a better idea of what is causing the issue.

Also, don't forget to take that statement out when you're done!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM