Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to communicate between a WCF service and separate threads?

The wording of the question doesn't necessarily do the issue justice...

  • I've got a client UI sitting on a local box with and a background windows service to support it while it performs background functions.
  • The client UI is just the presentation layer and the windows service does all the hard hitting action... so there needs to be communication between the two of them. After spending a while on google and reading best practices, I decided to make the service layer using WCF and named pipes.
  • The client UI is the WCF client and the windows service acts as the WCF host (hosting locally only) to support the client.

So this works fine, as it should. The client UI can pass data to the WCF host. But my question is, how do I make that data useful? I've got a couple engines running on the windows service/WCF host but the WCF host is completely unaware of the existence of any background engines. I need the client's communications requests to be able to interact with those engines.

Does anybody have any idea of a good design pattern or methodology on how to approach facilitating communication between a WCF host and running threads?

like image 595
jermny Avatar asked Oct 23 '22 15:10

jermny


1 Answers

I think that your best bet is to have some static properties or methods that can be used to interchange data between the service threads/processes and the WCF service.

Alternatively, the way that we approach this is through the use of a database where the client or wcf service queues up requests for the service to respond to and the service, when it is available, updates the database with the responses to those requests. The client then polls the database (through WCF) on a regular basis to retrieve the results of any outstanding requests.

For example, if the client needs a report generated, we fire off a request through WCF and WCF creates a report generation request in the database.

The service responsible for generating reports regularly polls this table and, when it finds a new entry, it spins off a new thread/process that generates the report.

When the report has completed (either successfully or in failure), the service updates the database table with the result.

Meanwhile, the client asks the WCF service on a regular basis if any of the submitted reports have completed yet. The WCF service in turn polls the table for any requests that have been completed, but not been delivered to the client yet, gathers the information from them, and returns them to the client.

This mechanism allows us to do a couple of things:

1) We can scale the number of services processing these requests across multiple physical/virtual machines as the workload increases.

2) A given service can support numerous clients.

3) Through the WCF interface, we can extend this support to any client platform that we choose to support (web, win, tablet, phone, etc).

Forgot to mention:

Just because we elect to use a database doesn't mean that you have to in order to implement this pattern. You can easily implement the same functionality by creating a static request collection that the WCF service and worker service access in much the same way that we use the database.

You will just need to be very careful about properly obtaining and releasing locks on the static properties to avoid cross-thread collisions or deadlocks.

like image 80
competent_tech Avatar answered Oct 27 '22 11:10

competent_tech