编写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。
接着再次生成项目,并进行安装,然后,在服务管理器中就看到两个服务了。
这么个简单的实验,再次验证了:一个服务安装器只能用于安装一个服务,一个服务必须进行安装后才能启动。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战