码迷,mamicode.com
首页 > Windows程序 > 详细

编写Windows服务疑问2:探索服务与安装器的关系

时间:2016-01-21 23:04:37      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:

首先,来弄两个服务,一个叫“飞机”,一个叫“火车”。

    public class FeiJiService : ServiceBase
    {

        public FeiJiService()
        {
            ServiceName = "Fei_ji";
        }


    }

    public class HuoCheService : ServiceBase
    {
        public HuoCheService()
        {
            ServiceName = "Huo_che";
        }
    }

 

用于演示,服务很单,接着,匹配安装器。

    [RunInstaller(true)]
    public class SelfInstaller : Installer
    {
        public SelfInstaller()
        {
            ServiceInstaller fjinstaller = new ServiceInstaller();
            fjinstaller.ServiceName = "Fei_ji";
            fjinstaller.Description = "国产飞机 -- 008";
            fjinstaller.DisplayName = "飞机";
            Installers.Add(fjinstaller);

            ServiceProcessInstaller processinstaller = new ServiceProcessInstaller();
            processinstaller.Account = ServiceAccount.LocalSystem;
            Installers.Add(processinstaller);
        }
    }

这里我捣了个鬼,只安装了“飞机”服务,“火车”服务没有安装。

 

最后,偏偏在Main入口点处运行两个服务。

        static void Main()
        {
            ServiceBase[] svs =
            {
                    new FeiJiService(),
                    new HuoCheService()
                };
            ServiceBase.Run(svs);
        }

 

咱们就是来验证一下,没有被安装的服务到底能不能运行。

现在,执行installutil xxx.exe进行安装,安装后,在服务管理器中只看“飞机”,没看到“火车”。

技术分享

显然,目前只能启动“飞机”服务,而“火车”服务不在服务列表中。

看来,只有安装后的服务才能启动。

 

下面,再次修改安装器代码,把“火车”服务也安装上。

        public SelfInstaller()
        {
            ServiceInstaller fjinstaller = new ServiceInstaller();
            fjinstaller.ServiceName = "Fei_ji";
            fjinstaller.Description = "国产飞机 -- 008";
            fjinstaller.DisplayName = "飞机";
            Installers.Add(fjinstaller);

            ServiceInstaller hcinstaller = new ServiceInstaller();
            hcinstaller.ServiceName = "Huo_che";
            hcinstaller.Description = "国产列车";
            hcinstaller.DisplayName = "火车";
            Installers.Add(hcinstaller);

            ServiceProcessInstaller processinstaller = new ServiceProcessInstaller();
            processinstaller.Account = ServiceAccount.LocalSystem;
            Installers.Add(processinstaller);
        }

 

然后,把刚才安装的服务卸载掉,执行installUtil /u xxx.exe。

接着再次生成项目,并进行安装,然后,在服务管理器中就看到两个服务了。

技术分享

 

这么个简单的实验,再次验证了:一个服务安装器只能用于安装一个服务,一个服务必须进行安装后才能启动

 

示例代码下载地址

 

编写Windows服务疑问2:探索服务与安装器的关系

标签:

原文地址:http://www.cnblogs.com/tcjiaan/p/5149201.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!