我可以在一个windows服务中托管多个不同的windows服务吗?

时间:2021-01-01 16:49:45

I have a cluster of legacy windows service. All of this services is installed on several servers, but the service may be down for some reasons, such as memory leak, filesystem exceptions and sql connection exceptions. So we have to remote the server and check the status of the service daily. Although restore action is added. But it is still not robust, controllable.

我有一组遗留的windows服务。所有这些服务都安装在多个服务器上,但是由于某些原因,服务可能会被关闭,比如内存泄漏、文件系统异常和sql连接异常。因此,我们必须远程访问服务器并检查服务的状态。虽然还添加了还原操作。但它仍然不是稳健的,可控的。

The solution I want to find is, integrate all of those exist services to one windows service . This host service can run the sub services and scheduled them and monitor them, when errors occurred, my host service can restore , log the error to database and send email to related people. The solution also can make me to schedule the service, such as stop , set periodically. So I want to know is there a way I don't need to modify the assembly of the exist windows service which inherit from servicebase and integrate them to my framework. I thought that the OS can host the sub service , there must a way that my Host service or frame work can also host the sub service and schedule them too.

我想要找到的解决方案是,将所有这些现有服务集成到一个windows服务中。这个主机服务可以运行子服务并调度它们并监视它们,当错误发生时,我的主机服务可以恢复,将错误记录到数据库,并向相关人员发送电子邮件。解决方案还可以让我安排服务,如停止,定期设置。所以我想知道是否有一种方法,我不需要修改现有windows服务的程序集,它从servicebase继承并将它们集成到我的框架中。我认为OS可以承载子服务,必须有一种方式,我的主机服务或框架工作也可以托管子服务,并安排它们。

My final target is there is only one windows service , it is my host service. Other sub service runs under my service without installed to the OS.

我的最终目标是只有一个windows服务,它是我的主机服务。其他的子服务在我的服务下运行,没有安装到操作系统。

I have dug some related thread below, but they can't fit my requirements:

我已经在下面挖了一些相关的线索,但是他们不符合我的要求:

2 个解决方案

#1


0  

When you say services do you mean proper services or executables converted into services? If it is the latter, you've got full control over it. You could spawn the subservices as threads or even processes. The big problem is killing off the processes. With every new release of Windows OS, they add more and more security features which makes it difficult to kill off processes.

当您说服务时,您指的是正确的服务或被转换为服务的可执行程序吗?如果是后者,你就能完全控制它。您可以将子服务派生为线程甚至进程。最大的问题是消灭这些过程。随着Windows操作系统的每一个新版本的发布,它们增加了越来越多的安全特性,这使得关闭进程变得非常困难。

Threads may be a problem in that if one thread dies due to memory leaks, it affects the program. The whole program may have to kill itself off and restart.

线程可能是一个问题,如果一个线程死于内存泄漏,它会影响程序。整个程序可能会自动关闭并重新启动。

A note of warning: services are run as a user called "Local User". You cannot login as "Local User" and even as the top administrator (called Administrator), you can't kill off "Local User" services or processes. Only the "Local User" can kill them off.

警告:服务作为用户运行,称为“本地用户”。您不能以“本地用户”的身份登录,即使作为最高管理员(称为管理员),也不能删除“本地用户”服务或流程。只有“本地用户”才能杀死他们。

Have you had a look at shareware products like firedaemon? They do something similar.

您看过firedaemon之类的共享软件产品吗?他们做一些类似的事情。

#2


0  

So i want to know is there a way i don't need to modify the assembly of the exist windows service which inherit from servicebase and integrate them to my framework

所以我想知道是否有一种方法,我不需要修改现有windows服务的程序集,它从servicebase继承并将它们集成到我的框架中。

It sounds like you don't want to modify existing code, so I would say by far the simplest and most effective solution would be to write a separate service which monitors the other ones.

这听起来好像你不想修改现有的代码,所以我想说,到目前为止,最简单和最有效的解决方案是编写一个单独的服务,监视其他服务。

The ServiceController class, contains all the functionallity you would need to check the status of other services without resorting to sc or another utiltities calls.

ServiceController类包含了检查其他服务状态所需的所有功能,而不必求助于sc或其他utiltities调用。

You can maintain a .txt file or an .xml file with a list of the services you want to monitor.

您可以保存一个.txt文件或.xml文件,其中包含您想要监视的服务的列表。

string[] servicesToWatch = GetServiceList();
foreach (string service in servicesToWatch)
{
    try
    {
        ServiceController sc = new ServiceController(service);
        if (sc.Status != ServiceStatus.Running)
        {
            sc.Start();
            sc.WaitForStatus(ServiceStatus.Running, TimeSpan.FromMinutes(2));
            // log to database that service was restarted
        }
    }
    catch (Exception e)
    {
        // log to database that service could not be started
        // send emails to necessary people
    }
}

You can then put the above code in a timer or in a while loop that checks every x minutes.

然后,您可以将上面的代码放到一个计时器中,或者在每隔x分钟检查一次的while循环中。

#1


0  

When you say services do you mean proper services or executables converted into services? If it is the latter, you've got full control over it. You could spawn the subservices as threads or even processes. The big problem is killing off the processes. With every new release of Windows OS, they add more and more security features which makes it difficult to kill off processes.

当您说服务时,您指的是正确的服务或被转换为服务的可执行程序吗?如果是后者,你就能完全控制它。您可以将子服务派生为线程甚至进程。最大的问题是消灭这些过程。随着Windows操作系统的每一个新版本的发布,它们增加了越来越多的安全特性,这使得关闭进程变得非常困难。

Threads may be a problem in that if one thread dies due to memory leaks, it affects the program. The whole program may have to kill itself off and restart.

线程可能是一个问题,如果一个线程死于内存泄漏,它会影响程序。整个程序可能会自动关闭并重新启动。

A note of warning: services are run as a user called "Local User". You cannot login as "Local User" and even as the top administrator (called Administrator), you can't kill off "Local User" services or processes. Only the "Local User" can kill them off.

警告:服务作为用户运行,称为“本地用户”。您不能以“本地用户”的身份登录,即使作为最高管理员(称为管理员),也不能删除“本地用户”服务或流程。只有“本地用户”才能杀死他们。

Have you had a look at shareware products like firedaemon? They do something similar.

您看过firedaemon之类的共享软件产品吗?他们做一些类似的事情。

#2


0  

So i want to know is there a way i don't need to modify the assembly of the exist windows service which inherit from servicebase and integrate them to my framework

所以我想知道是否有一种方法,我不需要修改现有windows服务的程序集,它从servicebase继承并将它们集成到我的框架中。

It sounds like you don't want to modify existing code, so I would say by far the simplest and most effective solution would be to write a separate service which monitors the other ones.

这听起来好像你不想修改现有的代码,所以我想说,到目前为止,最简单和最有效的解决方案是编写一个单独的服务,监视其他服务。

The ServiceController class, contains all the functionallity you would need to check the status of other services without resorting to sc or another utiltities calls.

ServiceController类包含了检查其他服务状态所需的所有功能,而不必求助于sc或其他utiltities调用。

You can maintain a .txt file or an .xml file with a list of the services you want to monitor.

您可以保存一个.txt文件或.xml文件,其中包含您想要监视的服务的列表。

string[] servicesToWatch = GetServiceList();
foreach (string service in servicesToWatch)
{
    try
    {
        ServiceController sc = new ServiceController(service);
        if (sc.Status != ServiceStatus.Running)
        {
            sc.Start();
            sc.WaitForStatus(ServiceStatus.Running, TimeSpan.FromMinutes(2));
            // log to database that service was restarted
        }
    }
    catch (Exception e)
    {
        // log to database that service could not be started
        // send emails to necessary people
    }
}

You can then put the above code in a timer or in a while loop that checks every x minutes.

然后,您可以将上面的代码放到一个计时器中,或者在每隔x分钟检查一次的while循环中。