在docker中运行c#worker服务有意义吗? [英] Does it make sense to run a c# worker service in docker?

查看:36
本文介绍了在docker中运行c#worker服务有意义吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在docker中开发一个多容器应用程序.其中一项服务是C#中运行时间长的控制台应用程序,该应用程序基本上是对数据库进行一些轮询并将数据发送到e服务器的.我只是通过添加以下语句来保持服务运行:

I am developing a multi-container app in docker. One of the services is a long-running console application in C# which basically does some polling on a database and sending the data to e server. I just keep the service running by adding this statement:

while(true);

现在,我正在考虑将此服务更改为.NET Core辅助服务(甚至Windows服务,因为我仅在Windows主机上使用Windows容器).我已经阅读了一些有关工作者服务的优点的文章,但是当涉及到容器化应用程序时,它们似乎都显得过时了,因为无论如何我的容器都是作为后台服务"运行的.(而且我每个容器/图像只使用一项服务).所以我的问题是:

Now I'm thinking of changing this service to a .NET Core worker service (or even windows service, since I am only using windows containers on windows hosts). I have read some articles about the pros of worker services, but they seem all quite obselete when it comes to a containerized application since my container anyway is running as kind of a "background service" (and i only use one service per container/image). So my question is:

与在docker中运行控制台应用程序相比,在docker中运行核心工作程序服务是否有优点或缺点?

Is there any benefit or drawback when running a core worker service in docker compared to running a console app in docker?

更新:通过工人服务"我指的是.NET Core 3.x中可用的新工作程序服务模板:https://www.stevejgordon.co.uk/what-are-dotnet-worker-services

Update: With "worker service" I refer to the new worker service template available in .NET Core 3.x: https://www.stevejgordon.co.uk/what-are-dotnet-worker-services

推荐答案

如果您始终要在容器中运行,请使用控制台应用程序.我认为作为服务运行没有固有的好处,因为在适当的编排(例如Kubernetes)下,容器应该被视为短暂的.另外,如果您将.NET Core 3.1.x应用程序简化为Linux或Windows容器(例如控制台),则可以减少摩擦.

If you are always going to run in a container, then go with a console app. I see no inherent benefit of running as a service since containers, under proper orchestration such as Kubernetes, should be considered ephemeral. Also, you will have less friction in running your .NET Core 3.1.x application as a Linux or Windows container if you keep it simple i.e. console.

此外,我将在控制台中使用以下行来确保其与容器分配的CPU配合良好:

Also, I would use the following line in your console to ensure it plays nice with the allocated CPU for the container:

while(true)
{
    Thread.Sleep(1 * 1000);
}

这篇关于在docker中运行c#worker服务有意义吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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