带有工作线程的 WCF 服务 - 如何设计? [英] WCF Service with Worker Thread - how to design?

查看:19
本文介绍了带有工作线程的 WCF 服务 - 如何设计?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 WCF 服务,它为一些客户提供服务.设计是:

I have a WCF Service that serves some clients. The design is :

  • WCF 服务层
  • 业务逻辑层
  • 数据访问层(LINQ-To-Entities)

我需要一个工作线程来对数据库进行一些连续的工作(查找新记录,如果找到的话 - 以推送"方式向客户端发送信息,这意味着 - 客户端将托管一个服务所以它可以从这个工作线程接收推送"通知).

I need to have a worker thread that does some continuous work on the database (looks for new records, and if finds any - sends information to the client in a 'Push' manner, meaning - the client will be hosting a service so it can receive 'push' notifications from this worker thread).

我将在 Windows 服务上托管 WCF 服务.

I will host the WCF service on a windows service.

问题是:在我的设计中,这个工作线程应该放在哪里?它是否应该与 WCF 服务一起在 Windows 服务的Program.cs"的Main()"中生成?(这意味着它应该是 WCF 服务程序集的一部分)或者它应该是业务逻辑层的一部分 - 因此是业务逻辑"程序集的一部分?

Question is : Where do I fit this worker thread in my design ? Should it be spawned in the 'Main()' of the Windows Service's 'Program.cs' along-side the WCF service ? (and this means it should be part of the WCF Service assembly) Or should it be part of the Business Logic layer - and thus part of the 'Business Logic' assembly ?

我的想法:

推荐答案

为什么它需要成为任一程序集的一部分?我会将这个工作线程完全托管在它自己的进程中.例如,将其托管在单独的 Windows 服务中.

Why does it need to be part of either assembly? I would host this worker thread in it's own process entirely. Host it in a separate windows service for example.

然后它可以轮询数据库并将数据推送到客户端.

It can then poll the database and push data to the clients.

更新

您的设计将三种不同的操作结合在一起.首先,您有数据库读取操作.然后你有数据库更新操作.然后你有数据库通知(或事件).

Your design couples together three distinct kind of operations. First you have the database read operations. Then you have the database update operations. Then you have the database notifications (or events).

这些不同类型的操作要求中的每一种都应该相互分离.这使得整个架构更易于维护和理解.

Each of these different types of operation requirement should be decoupled from each other. This makes the whole architecture easier to maintain and understand.

例如,通过解耦读取操作,您可以决定是否完全使用服务接口.也许可以让您的客户端使用 ADO 直接连接数据库来执行选择操作?

For example, by decoupling the read operations you can make a decision about whether to use a service interface at all. Maybe it's possible to allow your clients to connect directly the database using ADO to perform select operations?

无论如何,无论您是否使用服务,更新操作都应该离线.没有充分的理由将读取和写入操作结合在一起.这还允许您减少数据库争用的可能性,并再次使一切变得简单.客户端向更新队列发送异步更新命令,然后更新服务更新数据库.

At any rate, whether you use a service or not, the update operations should be offlined. There is no good reason to couple together read and write operations. This also allows you to reduce the likelihood of database contention and again keeps everything simple. The client sends asynchronous update commands to the update queue and then the update service updates the database.

这就是我的想法:

这篇关于带有工作线程的 WCF 服务 - 如何设计?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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