双击启动Windows服务。

4

如何使我的Windows服务按照以下方式工作...

1.) 安装后自动启动

2.) 即使我们只是双击可执行文件,也可以自动启动

换句话说,我不想使用“NET START”、“SC”命令,并且不想通过服务控制台启动它。我只希望我的服务能够自动安装和自动启动...并在双击可执行文件时自动启动。

谢谢。

4个回答

4

请查看ServiceController类。

您可以在commited事件中像这样使用它:

[RunInstaller(true)]
public class ServiceInstaller : Installer
{
    string serviceName = "MyServiceName";

    public ServiceInstaller()
    {
        var processInstaller = new ServiceProcessInstaller();
        var serviceInstaller = new ServiceInstaller();

        processInstaller.Account = ...;
        processInstaller.Username = ...;
        processInstaller.Password = ...;

        serviceInstaller.DisplayName = serviceName;
        serviceInstaller.StartType = ServiceStartMode.Automatic;

        serviceInstaller.ServiceName = serviceName;

        this.Installers.Add(processInstaller);
        this.Installers.Add(serviceInstaller);

        this.Committed += new InstallEventHandler(ServiceInstaller_Committed);
    }

    void ServiceInstaller_Committed(object sender, InstallEventArgs e)
    {
        // Auto Start the Service Once Installation is Finished.
        var controller = new ServiceController(serviceName);
        controller.Start();
    }
}

我仍然需要使用“NET START”或“SC”... 如何通过双击来启动服务? - Josh
@Josh 你只需要双击可执行文件即可运行服务。你可以创建另一个可执行文件,使用ServiceController启动未启动的服务。你可以查看MS SQL Server是如何做到的。有一个单独的程序可以停止和启动服务,如果你不想使用MMC的话。 - Incognito

3

请看Topshelf项目(http://topshelf-project.com),消除在.NET中编写Windows服务的所有复杂性。它处理所有自注册并消除应用程序对服务代码的所有依赖。

它还是开源的,并托管在GitHub上,这使得它易于适应任何应用程序。

(完全披露,我是该项目的作者之一)


尝试了您的项目...但是SERVICE参数不起作用。它就像一个普通的Windows服务。它显示“--------------------------- Windows Service启动失败

无法从命令行或调试器启动服务。必须首先使用installutil.exe安装Windows服务,然后使用ServerExplorer、Windows服务管理工具或NET START命令启动。

好的

”。
- Josh
你需要创建自己的EXE文件,可以按照这里的步骤进行操作:http://topshelf-project.com/documentation/getting-started/ - Chris Patterson
请查看我的问题链接: http://stackoverflow.com/questions/3256535/topshelf-difference-between-configureserviceinisolation-and-configureservice - Josh

1

您可以添加调用安装程序的命令行参数(使用ManagedInstallerClass.InstallHelper()),以及启动服务的代码...

 public class DataImportService : ServiceBase
 {
     // ----------- Other code -----------

     static void Main(string[] args)
     {
       if (args.Length == 0) 
       {
            InstallService(false, argValue); break;
            StartService();
       }
       else
       {
            string arg0 = args[0],
            switchVal = arg0.ToUpper(),
            argValue = arg0.Contains(":") ?
            arg0.Substring(arg0.IndexOf(":")) : null;

            switch (switchVal.Substring(0, 1))
            {
                //Install Service and run
                case ("I"): case ("-I"): case ("/I"):
                    InstallService(true, argValue); break;

                 // Start Service
                case ("S"): case ("-S"): case ("/S"):
                    StartService();
                default: break;

                 // Install & Start Service
                case ("IS"): case ("-IS"): case ("/IS"):
                    InstallService(false, argValue); break;
                    StartService();

                // Uninstall Service
                case ("U"): case ("-U"): case ("/U"):
                    InstallService(false, argValue); break;

                default: break;                   
            }
        }

     private static void InstallService(bool install,  string argFileSpec)
     {
        string fileSpec = Assembly.GetExecutingAssembly().Location;
        if (!String.IsNullOrEmpty(argFileSpec)) fileSpec = argFileSpec;
        // ------------------------------------------------------------
        string[] installerParams =
            install? new string[] { fileSpec } :
                     new string[] { "/u", fileSpec };
        ManagedInstallerClass.InstallHelper(installerParams);
     }

     private void StartService()
     {
        var ctlr  = new ServiceController();
        ctlr.ServiceName = "MyService";    // hard code the service name
        // Start the service
        ctlr.Start();           
     }
}

启动服务怎么样?我只需要双击即可启动服务… - Josh
@Josh,使用ServiceController()类启动和停止服务。您需要决定如何编写此主例程才能完成它...这取决于您... - Charles Bretana

0

我的这篇文章展示了如何使用-install选项从命令行安装Windows服务。你可以扩展这个逻辑,添加一个-start选项,并创建一个包含该选项的桌面快捷方式。


“-start”选项是我实际需要的。 - Josh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接