.NET 中长时间运行的有状态服务

发布于 2024-08-25 04:14:08 字数 814 浏览 6 评论 0 原文

我需要在 .NET 中创建一个服务,该服务在内存中维护(内部)状态,生成多个线程并且通常长时间运行。有很多选项 -

  • 好的旧 Windows 服务
  • Windows 通信服务
  • Windows Workflow Foundation

我真的不知道该选择哪个。大多数功能都在该服务使用的库中,因此该服务本身相当简单。

一方面,重要的是服务主机尽可能接近“简单工作”,其中不包括 Windows 服务。另一方面,重要的是服务不会因为没有外部活动而被主机关闭,这使得 WCF 有点“可怕”。至于 WF,它最大的卖点是能够创建流程,例如,嗯……工作流程,这是我不需要也不想要的东西。

总而言之,过多的微软技术让我有点困惑。

对于 .NET 中有状态、长期运行的服务的问题,我希望获得关于每种解决方案(或我没有提及的其他解决方案)的优缺点的帮助

谢谢,
阿萨夫

附注,
我正在使用 .NET 4。

编辑:

  • 主机“简单工作”的意思是,例如,我创建的服务在崩溃时会重新激活。
  • 我想这个问题的原因是我过去创建过 Windows 服务(我认为它是使用 Win32 API 的纯 C++ 编写的),并且我不想错过一些更简单的东西(如果有的话)。

感谢迄今为止所有的回复!
阿萨夫。

编辑2:

我将使用Windows服务,并可能在其中托管WCF服务以允许其他进程与其通信。

谢谢,
阿萨夫

I need to create a service in .NET that maintains (inner) state in-memory, spawns multiple threads and is generally long-running. There are a lot options -

  • Good-old Windows Service
  • Windows Communication Services
  • Windows Workflow Foundation

I really don't know which to choose. Most of the functionality is in a library used by this service, so the service itself is rather simple.

On one hand, it's important the service host is as close to "simply working" as possible, which excludes Windows Service. On the other hand, it's important that the service is not taken down by the host just because there's no external activity, which makes WCF kind o' "scary". As for WF, it's strongest selling point is the ability to create processes as, um..., workflows, which is something I don't need nor want.

To sum it up, the plethora of Microsoft technologies got me a bit confused.

I'd appreciate help regarding the pros and cons of each solution (or other's I've failed to mention) for the problem of a stateful, long running service in .NET

Thanks,
Asaf

P.S.,
I'm using .NET 4.

EDIT:

  • What I mean by the host "simply working" is, for example, that the service I create be reactivated if it crashes.
  • I guess the reason for this question is that I've created Windows Services in the past (I think it was in plain C++ with Win32 API), and I don't want to miss out on something simpler if there's is such as thing.

Thanks for all the replies thus far!
Asaf.

EDIT 2:

I'll use a Windows Service, and might host a WCF service inside it to allow other processes to communicate with it.

Thanks,
Asaf

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

醉酒的小男人 2024-09-01 04:14:08

根据您的描述,您最好的选择是#1,一个很好的旧 Windows 服务。您可以为其分配工作,它可以根据您的需要运行,并且可以生成线程。不确定“简单地工作”是什么意思,或者为什么这会使 Windows 服务无法运行。

或者,您可以制作一个简单的控制台应用程序,但您必须自己处理管理(重新启动等)。

WCF 本质上是通过 Web 进行的 .NET 远程处理,它本身不处理长时间运行的进程。 Workflow Foundation 可以处理长时间运行的工作流,但仅此而已。

Based on your description, your best bet is #1, a Good Old Windows Service. You can assign work to it, it can run as long as you want, and it can spawn threads. Not sure what you mean by "simply working," or why that puts a Windows Service out of the running.

You could, alternatively, make a simple console app, but you'd have to handle the management (restarts, etc) yourself.

WCF is essentially .NET remoting over the web, it doesn't handle long-running processes by itself. Workflow Foundation can handle long-running workflows, but only that.

猫弦 2024-09-01 04:14:08

根据您的要求“内存中的(内部)状态,产生多个线程并且通常长时间运行”,实际上有两个选项:

  1. 控制台应用程序
  2. Windows 服务

提到的其他两个是这些选项的派生物。第一个太原始了,让我们忘记这一点。但为了便于调试,我建议在 Main 中检查 Environment.UserInteractive ,如果为 true,则作为控制台应用程序运行,否则作为服务运行。所以你的选择应该是第二。

WCF 是通信框架,与长期运行的服务无关。 IIS 可以托管 WCF 应用程序,但没有多线程和内存数据,它是作为 Web 服务的单个调用。 WF 是工作流框架,并且与服务无关。它可以帮助实现复杂的流程逻辑。

WCF和WF都可以在Windows服务中使用。

According to your requirements "(inner) state in-memory, spawns multiple threads and is generally long-running" there are really two options:

  1. Console application
  2. Windows service

Other two mentioned are derivatives of these. First one is too primitive and let's forget about this. But for ease of debug i recommend in Main check Environment.UserInteractive and if true run as console app, else as service. So your choice should be 2nd.

WCF is communication framework and has nothing about long running service. IIS can host WCF apps, but no multithreading and inmemory data there, it is single call as web service. WF is workflow framework and again has nothing about services. It can help implementing complex flow logic.

Both WCF and WF can be used in Windows service.

踏月而来 2024-09-01 04:14:08

由于您似乎不需要任何与工作流相关的功能,因此我投票给 WCF 中的持久服务。这是 .NET 3.5 中的新功能,它允许 WCF 服务将其状态持久保存到基于提供程序的“持久性存储”中,通常是 SQL Server 后端数据库(但也有一个基于文件系统的提供程序,并且它是可扩展的 - 您可以编写如果需要的话你自己的)。

查看有关此主题的一些优秀博客文章:

WCF 是一个强大而强大的通信库,它使您免于许多麻烦和细节,当你从头开始创建所有这些时,你必须处理这些问题。

WCF 可以托管在 IIS 中(尽管有其所有缺陷),或者您也可以将 WCF 服务粘贴到 Windows NT 服务中,并让它在计算机启动时启动并运行,而无需任何人登录。

Since you don't seem to need any workflow-related features, my vote goes out to Durable Services in WCF. This is new in .NET 3.5, and it allows WCF serviecs to persist their state into a provider-based "persistance store", typically a SQL Server backend database (but there's also a filesystem-based provider, and it's extensible - you can write your own if need be).

Check out some excellent blog posts on this topic:

WCF is a great and powerful communication library which frees you from a lot of hassle and details that you'd have to deal with when creating all of this from scratch.

WCF can be hosted in IIS (with all its deficiencies), or you can stick your WCF service into a Windows NT Service and have it start up and run when the machine boots, without anyone being logged on.

烛影斜 2024-09-01 04:14:08

我会推荐很好的 ol windows 服务。为此,我使用 C# 编写了许多服务。它们可以生成线程,永远运行,并且经过精心设计,可以执行您想要的操作。您可以直接在 Visual Studio 中创建 .net windows 服务。它是 .net 项目类型之一。

WCS 和 WCF 都会做您想做的事情,但需要一些额外的工作,而且实际上并不是为您想要做的事情而设计的。

I would suggest the good ol windows service. I have written many many services using C# for these very purposes. They can spawn threads, run for an eternity, and are well designed to do what you want. You can create a .net windows service right in Visual Studio. It's one of the .net project types.

WCS and WCF will both do what you want but require some extra work and really weren't designed for what you seem to want to do.

被你宠の有点坏 2024-09-01 04:14:08
  1. 尽可能“简单地工作” - 不包括 Windows 服务...
  2. 不会因不活动而被关闭...可怕 - WCF ...
  3. 不需要工作流,也不想要它 - Workflow Foundation ...

似乎没有多种选择,尽您所能! ;P

  • 一个简单的 EXE 来完成这项工作并通过 Windows 任务计划程序进行计划怎么样?您可以从应用程序内部管理多线程。这就像控制台应用程序一样简单。

编辑 我的第一选择也是 Windows 服务,但通过调度程序更简单。

  1. as "simply working" as possible - which excludes Windows Service...
  2. not taken down due to inactivity... scary - WCF...
  3. no need of a workflow, nor want it - Workflow Foundation...

It seems there's not as much as options as you would like to believe it! ;P

  • What about a simple EXE that does the job, and that is scheduled through the Windows Task Scheduler? You manage the multithreading from inside your app. This would be as simple as a console application.

EDIT My first choice would be a Windows Service as well, but it is simpler through the scheduler.

许你一世情深 2024-09-01 04:14:08

也许看看Quartz.Net?它更像是一个调度程序,但您可以在多个线程上启动作业等。我使用它代替 Windows 服务并取得了巨大成功。

Maybe check out Quartz.Net? Its more of a scheduler but you can start jobs etc on multiple threads. I used this instead of a Windows Service with great success.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文