zoukankan      html  css  js  c++  java
  • 编写Windows服务疑问2:探索服务与安装器的关系

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

        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。

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

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

    示例代码下载地址

  • 相关阅读:
    查询数据库中的相同值得所有表跟字段【存储过程】
    一些常用的SQL语句
    添加网站本地映射
    ReSharper 2016.3.2 Ultimate 官方最新破解版
    C# 利用VS自带的WSDL工具生成WebService服务类
    Linux环境下docker搭建wordpress应用
    Appium环境搭建
    内联以及外联css,js文件的理解
    前端雅虎23条理解
    docker安装和使用
  • 原文地址:https://www.cnblogs.com/tcjiaan/p/5149201.html
Copyright © 2011-2022 走看看