简体   繁体   English

Dotnet Core Windows Service中的多个托管服务

[英]Multiple Hosted Services in Dotnet Core Windows Service

I have a windows service for netcore 2.0 which is based on this example: https://www.stevejgordon.co.uk/running-net-core-generic-host-applications-as-a-windows-service 我有一个基于此示例的netcore 2.0 Windows服务: https ://www.stevejgordon.co.uk/running-net-core-generic-host-applications-as-a-windows-service

The main hosted service in my case is subscribed to a message queue and processes the messages sequentially (an event is raised by the queue api each time a new message appears). 在我的情况下,主要的托管服务已订阅消息队列并按顺序处理消息(每次出现新消息时,队列api都会引发一个事件)。 If any messages cannot be processed they are moved to a different queue to be retried (x number of times, with exponential back-off). 如果无法处理任何消息,则将它们移到另一个队列以进行重试(x次,具有指数补偿)。 And so this needs to be done on a separate thread so as not to cause delays on the processing of the main queue 因此,这需要在单独的线程上完成,以免引起主队列处理的延迟

Is it ok to configure 2 HostedServices: 是否可以配置2 HostedServices:

var builder = new HostBuilder()
            .ConfigureServices((hostContext, services) =>
            {
                services.AddHostedService<MyMsgProcessingService>();
                services.AddHostedService<MyRetryService>();
            });

This does start and it does hit the StartAsync method (on the IHostedService interface) of each service 这不启动,它并击中StartAsync方法(上IHostedService每个服务的接口)

Or are you only allowed to have (is a good idea to have) one IHostedService implementation? 还是只允许拥有(一个好主意)一个IHostedService实现?

If two is OK, will the work on the slow service hold up the wok on the main service? 如果两个都可以,慢速服务上的工作是否会阻碍主服务上的炒锅?

If only one, is it best to just spin up another thread to do the work for the retry queue? 如果只有一个线程,那么最好只是启动另一个线程来为重试队列工作吗? And how would I ensure that the new thread lives for the whole time the service is running? 以及如何确保新线程在服务运行的整个过程中都有效? a while loop seems inappropriate as I'm just waiting for an event to be raised while循环似乎是不合适的,因为我只是在等待事件的发生

eg 例如

public Task StartAsync(CancellationToken cancellationToken)
{
    Task t = Task.Run(() => ProcessRetrys(), cancellationToken);

Or are you only allowed to have (is a good idea to have) one IHostedService implementation? 还是只允许拥有(一个好主意)一个IHostedService实现?

You can have as many as you want. 您可以根据需要拥有任意数量。 As long as you are okay with the fact that if the process goes down, everything in it (all of services) are too go down. 只要您对以下事实感到满意,那就是如果该进程出现故障,那么其中的所有内容(所有服务)也都将崩溃。

Second thing to consider is thread pool. 要考虑的第二件事是线程池。 Thread pool is common for all the services, so if one of them is hungry for threads, others could be impacted. 线程池对于所有服务都是通用的,因此,如果其中之一渴望使用线程,则其他服务可能会受到影响。

If two is OK, will the work on the slow service hold up the wok on the main service? 如果两个都可以,慢速服务上的工作是否会阻碍主服务上的炒锅?

Depends on if one service waits data from another. 取决于一项服务是否等待另一项服务的数据。 And on type of communication between them. 以及关于它们之间的通信类型。 If it's synchronous, then the slowest thread will be the bottleneck. 如果是同步的,则最慢的线程将成为瓶颈。

And how would I ensure that the new thread lives for the whole time the service is running? 以及如何确保新线程在服务运行的整个过程中都有效?

Create a foreground thread. 创建一个前台线程。 About . 关于 How to configure. 如何配置。 But you have to be careful and terminate such thread graceful when the app shuts down, handling it in StopAsync method invocation. 但是您必须要小心,并在应用程序关闭时优雅地终止此类线程,并在StopAsync方法调用中StopAsync处理。

a while loop seems inappropriate as I'm just waiting for an event to be raised 一会儿循环似乎是不合适的,因为我只是在等待事件的发生

Use non-blocking queues to communicate between services. 使用非阻塞队列在服务之间进行通信。 I can't give you a link on a specific implementation, because there are plenty of them, but you can start another question to help you find an appropriate one. 我无法给您提供特定实现的链接,因为其中有很多实现,但是您可以提出另一个问题来帮助您找到合适的实现。

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

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