Wcf – How to make WCF Service with a background worker thread

backgroundworkerwcf

I have a WCF service that all clients connect to in order to get notifications \ reminders (using a CALLBACK interface they implement). Currently the WCF service is self-hosted, but the plan is to have it hosted in a Windows Service.

The WCF service has a 'Publish', 'Subscribe' and 'Unsubscribe' operations.

I need to have a background worker thread of some sort poll an SQL server database table constantly [every XXX minutes], and look for certain 'reminder' rows. Once it finds them – it should notify all the connected clients.

I thought of 2 ways of achieving this.

.

METHOD A :

Have a separate EXE project (don't want it to be a console, so what should it be – a Windows Service ?) that will start and run a background thread. The background thread will connect to the 'Reminder' service as one of its clients. The background thread will poll the database, and once it finds something – it will send a 'Publish' message to the WCF service, that will make the WCF service send the reminder to all the subscribed clients.

.

METHOD B :

Somehow make the background thread run within the WCF service project, and when it detects a new reminder row in the database, somehow make it 'signal' the WCF service with the info, and the WCF service will then send this info to all subscribed clients.

.

Which method is better ? Any other suggestions ?

Best Solution

If this is a long running process, a windows service is the perfect solution. Your main Win Service thread will be polling the DB, queuing the results into some kind of supplier/consumer thread safe collection.

You can host a WCF service within the win service, which can then consume (remove) any results from the queue and pass them back to the client as requested (calls into the WCF will come in on their own thread)

This is a pretty common architecture, and not difficult to implement.

Related Question