本文介绍了Windows Service 应用架构疑惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一些 Windows 服务应用程序 (C#),它应该执行多个定期执行并相互绑定的任务,例如:

  1. 注意文件夹的变化.
  2. 将文件发送到网络服务 (Web API).
  3. 写入日志并将其保存在本地数据库中.

所以,如果我可以使用快速和肮脏"方法并使用 BackgroundWorker 类实现所有这些,或者我使用更复杂的东西,例如 任务 y 等(.NET Windows 服务 - 架构决策).或者有一些框架可以做到这一点.

任何有关该应用程序可能的现代和简洁结构的建议都会很高兴听到.非常感谢!

解决方案

听起来您需要在专用服务器上使用它.所以我的第一个建议是使用 TopShelf.它让整合现代 Windows 服务变得如此简单,可笑.

然后要执行您的问题中的步骤,我将使用某种工作流模式.Windows Workflow Foundation 似乎足够现代,可以满足这一需求.(就我个人而言,我只是使用责任链模式和 DI 容器作为我的快速和肮脏").

最后将其与启动工作流程的事件驱动方法相结合,例如 FileSystemWatcher,你甚至不需要担心后台工作人员等.

因此,做出这些架构决策后,您的服务将易于设置、运行、测试和安装 (TopShelf),并且您可以模块化地构建和单元测试您的服务(工作流模式).

I need to create some Windows Service App (C#) that should perform several periodically executed and binded to each other tasks like:

So I am wandering if I can just use "quickly and dirty" approach and implement all this using BackgroundWorker class or I have use more sophisticated stuff like Tasks y etc (.NET Windows Service - Architectural Decisions). Or event there are some frameworks to do it.

Any advice about possible modern and clean structure of that application would be nice to hear. Thank you very much!

解决方案

Sounds like you'll be needing this on a dedicated server. So my first suggestion is to use TopShelf. It makes putting together modern Windows services so easy its laughable.

Then to perform the steps in your question I'd use some sort of workflow pattern. Windows Workflow Foundation seems to be modern enough to cater for this. (Personally, I just use the chain-of-responsibility pattern with a DI container as my quick and "dirty").

Lastly combining that with an event-driven approach that kicks off the workflow, like FileSystemWatcher, you won't even need to worry about backgroundworkers etc.

So with those architecture decisions made, your service will be easy to setup, run, test and install (TopShelf), and you can build and unit test your service modularly (workflow pattern).

这篇关于Windows Service 应用架构疑惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 12:08