WCF Service with Worker Thread - how to design?

冷暖自知 提交于 2019-12-11 18:39:43

问题


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

  • WCF Service Layer
  • Business Logic Layer
  • Data Access Layer (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).

I will host the WCF service on a windows service.

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 ?

What I had in mind:


回答1:


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.

UPDATE

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.

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.

This is kind of what I am thinking:



来源:https://stackoverflow.com/questions/10238909/wcf-service-with-worker-thread-how-to-design

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!