【问题标题】:Console application as Windows service installation issue控制台应用程序作为 Windows 服务安装问题
【发布时间】:2018-01-14 22:38:35
【问题描述】:

我使用 ServiceBase 更改了我的控制台应用程序以用作 Windows 服务。我使用以下命令安装了它。但是我没有在服务中找到该服务。我检查了它说的日志

“在 C:\Test\MyService.exe 程序集中找不到具有 RunInstallerAttribute.Yes 属性的公共安装程序”

如何为控制台应用程序创建安装程序?请告诉我。

"C:\Windows\Microsoft.NET\Framework\v4.0.30319\installutil.exe" "c:\MyService.exe"


    using System.ServiceProcess;
    public static class Program
    {
        public static bool Cancelled { get; set; }


        #region Nested classes to support running as service
        public const string ServiceName = "MyService";

        public class Service : ServiceBase
        {
            public Service()
            {
                ServiceName = Program.ServiceName;
            }

            protected override void OnStart(string[] args)
            {
                Program.Start(args);
            }

            protected override void OnStop()
            {
                Program.Stop();
            }
        }
        #endregion


        static void Main(string[] args)
        {
            if (!Environment.UserInteractive)
                // running as service
                using (var service = new Service())
                    ServiceBase.Run(service);
            else
            {
                // running as console app
                Start(args);

                Console.WriteLine("Press any key to stop...");
                Console.ReadKey(true);

                Stop();
            }

        }

        private static void Start(string[] args)
        {
            // onstart code here
            try
            {
              SaveMessage();
            }
            catch (Exception e)
            {
        LogError();
            }
        }

        private static void Stop()
        {
            // onstop code here
            DisposeAll();
        }
}

【问题讨论】:

  • 见this question - 虽然我个人会使用Topshelf - “通过引用Topshelf,您的控制台应用程序成为一个具有全面命令集的服务安装程序-line 选项,用于安装、配置和运行您的应用程序作为服务。”

标签: c# visual-studio-2015 windows-services


【解决方案1】:

我最喜欢的安装服务的方式是使用SC 命令行实用程序。

Official docs

完整的语法(吓唬所有人!)

sc [<ServerName>] create [<ServiceName>] [type= {own | share | kernel | filesys | rec | interact type= {own | share}}] [start= {boot | system | auto | demand | disabled}] [error= {normal | severe | critical | ignore}] [binpath= <BinaryPathName>] [group= <LoadOrderGroup>] [tag= {yes | no}] [depend= <dependencies>] [obj= {<AccountName> | <ObjectName>}] [displayname= <DisplayName>] [password= <Password>]

简单来说,

SC create YourServiceName start= auto binPath= "path/to/your/exe" DisplayName= "Your Display Name"

要删除服务,命令是

SC delete YourServiceName

上述命令需要在具有管理员权限的命令提示符下运行。请注意“=”号后面的空格很重要。

相关 SO post

【讨论】:

    【解决方案2】:

    我相信你需要从 System.Configuration.Install.Installer 扩展

    类似

    public class ServiceRegister: Installer 
    {
    
        public ServiceRegister() 
        {
            ServiceProcessInstaller serviceProcessInstaller =
                            new ServiceProcessInstaller();
            ServiceInstaller serviceInstaller = new ServiceInstaller();
    
            #if RUNUNDERSYSTEM
            serviceProcessInstaller.Account = ServiceAccount.LocalSystem;
            #else
            // should prompt for user on install
            processInstaller.Account = ServiceAccount.User;
            processInstaller.Username = null;
            processInstaller.Password = null;
            #endif
    
             serviceInstaller.DisplayName = "SomeName";
            serviceInstaller.StartType = ServiceStartMode.Manual;
            serviceInstaller.ServiceName = "SomeName";
    
    
            this.Installers.Add(serviceProcessInstaller);
            this.Installers.Add(serviceInstaller);
    
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多