简体   繁体   English

如何将数据从安装程序 class 传递到 windows 服务的 OnStart() 方法

[英]How to pass data from an installer class to the OnStart() method of a windows service

[RunInstaller(true)]
public partial class AttendanceInstaller : Installer
{
   public string idString;
   public string hoursString;
 
   public AttendanceInstaller()
   {
       InitializeComponent();
   }
   
   public override void Install(IDictionary stateSaver)
   {
      base.Install(stateSaver);
      idString = Context.Parameters["DeviceID"];
      hoursString = Context.Parameters["Hours"];
    }    
}

    
public partial class Attendance : ServiceBase
{
   protected override void OnStart(string[] args)
   {
   
   }
}

I need to use idString variable of the AttendanceInstaller class in the onStart() method.我需要在onStart()方法中使用AttendanceInstaller class 的idString变量。 How can I do?我能怎么做? Thanks for your help.谢谢你的帮助。

I'm taking the risk of writing an answer but providing no solution.我冒着写答案但不提供解决方案的风险。 I believe what you are trying is not possible.我相信你正在尝试的是不可能的。 At least, in an immediate sense.至少,在直接意义上。 Your class AttendanceInstaller inherits from Installer.您的 class AttendanceInstaller 继承自 Installer。 The Installer class is used to install the service. Installer class 用于安装该服务。 It may get executed once.它可能会被执行一次。 Your class Attendance inherits from ServiceBase.你的 class Attendance 继承自 ServiceBase。 It implements a service by overriding the OnStart method.它通过覆盖 OnStart 方法来实现服务。 The OnStart method is called when the service is started through the Service Control Manager.当通过服务控制管理器启动服务时调用 OnStart 方法。 So you have two separated classes (intances if being purist) that may never share scope. So you should find an alternative way of passing data, perhaps with a file on disk.所以你有两个可能永远不会共享 scope 的独立类(如果是纯粹的实例)。所以你应该找到一种传递数据的替代方法,也许是磁盘上的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM