多个窗口服务于一体的exe [英] Multiple Windows Services in One exe

查看:109
本文介绍了多个窗口服务于一体的exe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想建立几个Windows服务,做不同的事情。举例来说,我需要的Windows服务,这将:

I am trying to build several Windows services to do different things. For instance, I need Windows services that will:


  1. 通过电子邮件发送的每日报告

  2. 定期清理一些归档信息每30分钟


的任务,我需要的Windows服务这样做是不同的,所以我真的不喜欢让他们都在同一个服务的想法。

The tasks I need the windows services to do are distinct so I don't really like the idea of having them all in one service.

我有这么远在Visual Studio 2008中的一个项目我已经创建了一个窗口服务,我已经设置了一个计时器上的OnStart事件(它只是写入到一个文本文件中每5秒用于测试目的)。然后,我添加了一个安装程序项目,当我运行InstallUtil.exe,一切工作正常。

What I've got so far is a project in Visual Studio 2008. I've created a windows service, I've set up a timer on the OnStart event (it just writes to a text file every 5 seconds for testing purposes). I then added an Installer to the project and when I run InstallUtil.exe, everything works fine.

在问题出现时,我添加第二个窗口服务,在同一项目。我重新设置的OnStart代码,用相同的日志信息(略有不同,所以我可以告诉它服务写入日志)。随着第二个窗口服务,我改变了事件的主要Program.cs中的:

The problem comes in when I add a second windows service to the same project. I set up the OnStart code again, with the same logging info (slightly different so I can tell which service is writing to the log). With the second windows service, I changed the Main event in Program.cs from:

    static void Main(string[] args)
    {
        ServiceBase[] ServicesToRun = new ServiceBase[] 
        { 
            new Service1()
        };

        ServiceBase.Run(ServicesToRun);
    }



to:

    static void Main(string[] args)
    {
        ServiceBase[] ServicesToRun = new ServiceBase[] 
        { 
            new Service1(),
            new Service2()
        };

        ServiceBase.Run(ServicesToRun);
    }



在这一点上,没有编译时错误,但该客服2服务永远做任何事情......伐木任务永远不会触发。

At this point, there are no compile time errors, but the Service2 service never does anything...the logging task never fires.

我把范围缩小到第二服务有没有安装程序与它相关的事实。然后,我尝试添加一个安装我的第一个服务做的方式(即用鼠标右键单击该服务的设计师,并点击添加安装程序)。现在,当我去Proj​​ectInstaller.cs文件,还有另外的ServiceInstaller那里(serviceInstaller2)。

I narrowed it down to the fact that the second service has no "Installer" associated with it. I then tried to add an Installer the way I did with the first service (i.e., right click on the service designer, and click "Add Installer"). Now, when I go to the ProjectInstaller.cs file, there is another serviceInstaller there (serviceInstaller2).

现在,当我生成项目,并尝试安装的服务,以及我去服务控制面板窗口,我尝试启动服务1,我收到以下错误信息:

Now when I build the project and try to install the services, and I go to the "Services" control panel window, and I try to start Service1, I get the following error message:

Windows无法。在本地计算机上的服务1服务

Windows could not start the Service1 service on the Local Computer.

错误1083:此服务被配置为运行时不会实现服务的可执行程序

Error 1083: The executable program that this service is configured to run in does not implement the service.

我得到同样的错误消息,如果我尝试启动服务2以及(与错误消息指出,当然服务2,除外)。

I get the same error message if I try to start Service2 as well (with the exception that the error message identifies Service2, of course).

时有什么我为了得到从一个exe文件运行两个服务?

Is there something I am missing in order to get two services running from one exe?

推荐答案

我已经想通了,如何有一个可执行文件,但两个服务。每个服务安装到与自己的名称和启动/停止功能服务管理器。我认为这是你想要什么,对不对?下面是我所做的:

I have figured out how to have one executable but two services. Each service installs to the service manager with its own name and ability to start/stop. I think this is what you're wanting, correct? Here is what I did:


  1. 创建一个服务项目。

  2. 添加第二个服务于同一个项目(具有唯一服务名称)。

  3. 增加了一个安装程序这两个服务(ServiceA和ServiceB)。

  4. 在ProjectInstaller.Designer.vb文件我改变了Me.Installers.AddRange线同时显示服务安装的。 (Me.ServiceInstaller1,Me.ServiceInstaller2)

  5. 在主要服务(在我的情况ServiceA)的主要切入点,我的ServicesToRun变量设置为ServiceBase包含所有服务的数组我希望它运行(ServiceA和ServiceB)。这是因为服务管理的一个重要步骤将根据这里的参数数量的属性 - 要么允许同一exe文件或仅单个实例的多个实例。

  6. 添加一个安装项目并使用服务的输出。

  7. 添加使用的服务输出自定义操作。

  1. Created a service project.
  2. Added a second service to the same project (with a unique service name).
  3. Added an installer to both services (ServiceA and ServiceB).
  4. In the ProjectInstaller.Designer.vb file I changed the Me.Installers.AddRange line to show both of the service installers. (Me.ServiceInstaller1, Me.ServiceInstaller2)
  5. In the Main entry point of the main service (ServiceA in my case), I set the ServicesToRun variable to an array of ServiceBase containing all the services that I want it to run (ServiceA and ServiceB). This is an important step as the service manager sets a property based on the number of arguments here - either to allow multiple instances of the same exe or only a single instance.
  6. Add an installer project and use the output of Services.
  7. Add a custom action using the output from Services.

您可以在这里找到演示代码:
http://code.google.com/p/multi-service-install/

You can find the demo code here:
http://code.google.com/p/multi-service-install/

这篇关于多个窗口服务于一体的exe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆