.net - 如何让.NET Windows服务在安装后立即启动?

标签 .net windows-services installation

除了 service.StartType = ServiceStartMode.Automatic 我的服务在安装后不会启动

解决方案

将此代码插入我的 ProjectInstaller

protected override void OnAfterInstall(System.Collections.IDictionary savedState)
{
    base.OnAfterInstall(savedState);
    using (var serviceController = new ServiceController(this.serviceInstaller1.ServiceName, Environment.MachineName))
        serviceController.Start();
}

感谢 ScottTx 和 Francis B。

最佳答案

我发布了用 C# 创建 Windows 服务的分步过程 here 。听起来您至少到目前为止,现在您想知道安装后如何启动该服务。将 StartType 属性设置为“自动”将导致服务在重新引导系统后自动启动,但它不会(正如您所发现的)在安装后自动启动服务。

我不记得最初在哪里找到它(也许是 Marc Gravell?),但我确实在网上找到了一个解决方案,允许您通过实际运行服务本身来安装和启动服务。以下是分步操作:

  1. 按如下方式构建服务的 Main() 函数:

    static void Main(string[] args)
    {
        if (args.Length == 0) {
            // Run your service normally.
            ServiceBase[] ServicesToRun = new ServiceBase[] {new YourService()};
            ServiceBase.Run(ServicesToRun);
        } else if (args.Length == 1) {
            switch (args[0]) {
                case "-install":
                    InstallService();
                    StartService();
                    break;
                case "-uninstall":
                    StopService();
                    UninstallService();
                    break;
                default:
                    throw new NotImplementedException();
            }
        }
    }
    
  2. 这里是支持代码:

    using System.Collections;
    using System.Configuration.Install;
    using System.ServiceProcess;
    
    private static bool IsInstalled()
    {
        using (ServiceController controller = 
            new ServiceController("YourServiceName")) {
            try {
                ServiceControllerStatus status = controller.Status;
            } catch {
                return false;
            }
            return true;
        }
    }
    
    private static bool IsRunning()
    {
        using (ServiceController controller = 
            new ServiceController("YourServiceName")) {
            if (!IsInstalled()) return false;
            return (controller.Status == ServiceControllerStatus.Running);
        }
    }
    
    private static AssemblyInstaller GetInstaller()
    {
        AssemblyInstaller installer = new AssemblyInstaller(
            typeof(YourServiceType).Assembly, null);
        installer.UseNewContext = true;
        return installer;
    }
    
  3. 继续支持代码...

    private static void InstallService()
    {
        if (IsInstalled()) return;
    
        try {
            using (AssemblyInstaller installer = GetInstaller()) {
                IDictionary state = new Hashtable();
                try {
                    installer.Install(state);
                    installer.Commit(state);
                } catch {
                    try {
                        installer.Rollback(state);
                    } catch { }
                    throw;
                }
            }
        } catch {
            throw;
        }
    }
    
    private static void UninstallService()
    {
        if ( !IsInstalled() ) return;
        try {
            using ( AssemblyInstaller installer = GetInstaller() ) {
                IDictionary state = new Hashtable();
                try {
                    installer.Uninstall( state );
                } catch {
                    throw;
                }
            }
        } catch {
            throw;
        }
    }
    
    private static void StartService()
    {
        if ( !IsInstalled() ) return;
    
        using (ServiceController controller = 
            new ServiceController("YourServiceName")) {
            try {
                if ( controller.Status != ServiceControllerStatus.Running ) {
                    controller.Start();
                    controller.WaitForStatus( ServiceControllerStatus.Running, 
                        TimeSpan.FromSeconds( 10 ) );
                }
            } catch {
                throw;
            }
        }
    }
    
    private static void StopService()
    {
        if ( !IsInstalled() ) return;
        using ( ServiceController controller = 
            new ServiceController("YourServiceName")) {
            try {
                if ( controller.Status != ServiceControllerStatus.Stopped ) {
                    controller.Stop();
                    controller.WaitForStatus( ServiceControllerStatus.Stopped, 
                         TimeSpan.FromSeconds( 10 ) );
                }
            } catch {
                throw;
            }
        }
    }
    
  4. 此时,在目标计算机上安装服务后,只需使用 -install 命令行参数从命令行(像任何普通应用程序一样)运行服务即可安装并启动您的服务。

我想我已经涵盖了所有内容,但如果您发现这不起作用,请告诉我,以便我更新答案。

关于.net - 如何让.NET Windows服务在安装后立即启动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1195478/

相关文章:

.net - 在 .NET 中以编程方式检索数据库表创建脚本

c# - 我应该有单独的输入和输出对象吗?这个模式有名称吗?

c# - 排序数组

c# - Entity Framework 核心,更新数据库不考虑属性

c# - Windows 服务和更新 C# 控制台应用程序

installation - 通过 cargo : specified package has no binaries 安装箱子时出错

c# - 如何授予对 NETWORK SERVICE 帐户的 SQL 访问权限?

c - 在单独的 session 中运行应用程序?

macos - 如何安装多个包和后处理?

installation - 无法在新的 debian 8 jessie 上安装 docker