问题
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