错误时,我试图安装c# Windows服务

本文关键字:安装 Windows 服务 错误 | 更新日期: 2023-09-27 18:02:46

当我尝试安装我的c#窗口服务时,这就是我得到的,我是win服务应用程序的新手,请提及如果您需要进一步的信息,非常感谢:

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

错误时,我试图安装c# Windows服务

在服务项目的Program.cs文件中添加

Debugger.Launch();

作为

的第一行
private static void Main(string[] args)

当你启动服务时,它会询问你想要如何调试,选择你打开的Visual Studio参考。这样你就能更好地了解是什么导致了问题。

还有,写完后别忘了把那句话拿出来!