tags:

views:

125

answers:

4

From the little that I know about WCF it seems to be the right solution to a particular problem I have but I'd like to get some input on it.

Problem Description:

Clients sequentially access domain objects from a list roughly once ever 1-10 minutes (average is 2 mins), but no 2 clients should ever access the same object. Certain orderings should be maintained within the list.

Example Usage:

Client X -> WCFService::GetNextObject() -> Fetch from DB, sort
         <- return nextObject

Where X is [1,~200)

The list of objects is basically a DB table where each object is annotated with a priority. All objects with priority 1 should be the next ones passed out. The ordering from there depends on the priority level. E.g. priority 1 objects should be handed out chronologically based on a datetime on the object, whereas priority 4 objects need to be handed out alphabetically based on a string value on the object.

[Edit]Some points of clarification: there are other sorting schemes for different priority levels which need to reference associated records in myriad other tables - it seems overly complicated but there is a valid business case for it. Likewise, objects are added and removed from this list potentially very frequently (but the average is rarely).[/Edit]

I am looking at using WCF so that people from any geographical location can use it via LAN/WAN and it will ensure that the ordering is maintained and that no two clients get the same nextObject. Am I right in thinking that WCF is a good choice for this?

The one thing I wonder about is the state of a WCF Service? Or, better put: Is it stateless? Will each client's access to the service create a new logical WCF instance? And if so, then how is synchronization/locking normally approached?

It should be noted that everything else at the company here is developed on the .NET stack, so there's obviously an existing affinity for .NET technologies. It's not, however, mandatory by any means.

A: 

It sounds to me like you're just grabbing data from a service in a queue with multiple clients.

If this is a case, you should just use SQL Server and this is literally a queue table in a database. Each time a client dequeues an item from the table, it updates the priorities of the other artifacts.

Or better yet, just use an identity primary key and always grab the lowest id, and delete it when you grab it, or move it to another table if you need to for some reason.

Just use SQL Server or SQL Server Express if you want it free, and you get all the locking and etc for free.

Jimmy Hoffa
This is not good advice. The OP said that the users are geographically distributed. It is not good practice to open up SQL ports to the Internet.
Steven Sudit
--Insert joke about burying Jimmy Hoffa's answer here--
Marc Bollinger
I was assuming it would be an intranet, most places I've worked have had dev offices distributed about the globe and people wouldn't think twice about having a central database for use with a client we passed around. If he's referring to a publically released client yes this would be a terrible way to do it, sorry I didn't stipulate.
Jimmy Hoffa
+3  A: 

WCF is great for hiding the details of your server from your clients - it provides a layer of abstraction that will be very handy for handling a large number of clients. If this were a couple of people requesting data from the database then I would say that WCF would be overkill, but it sounds like this is the correct application of this technology.

Regarding the state of the WCF service, this is configurable. You can have the service run as a singleton, or have it generate a new instance with each session, or even each call.

Here is a (verbose!) explanation of WCF instances: http://msdn.microsoft.com/en-us/magazine/cc163590.aspx

Jake
A: 

I agree with Jake, but do want to add a few things:

  1. Clients can fail or go away, so it's important not to just hand them the object and lost all control of it. The usual way is to lease is to them for a period of time, so that if they don't complete the processing then the object goes right back into the queue. In your case, I suspect it would go back into the front of the queue, but it's more commonly the end. You also want to track retries in case some object is toxic and cannot be processed.

  2. Unless all of your clients are .NET, you're going to want to stick to the more universal parts of WCF.

Steven Sudit
What do you mean by "more universal parts"? Isn't WCF standard compliant with SOAP ?
SiN
@SiN: Yes, I meant SOAP. WCF can do SOAP, but it also supports not-HTTP channels and offers a number of other options that, as far as I can tell, are not part of SOAP.
Steven Sudit
+1  A: 

It seems like a solution involving WCF and MSMQ might fit your requirements. You can use MSMQ to store the next items that need to get served to the clients, while your WCF service can be the front-end for MSMQ so that your clients don't have to interact with it directly. Since your state is being maintained in MSMQ, your WCF service can remain stateless.

Dr. Wily's Apprentice
They *could* use MSMQ but hardly need to. The database is more than capable of supporting a priority queue.
Steven Sudit
It's a possible solution; and while the asker did mention a queue implementation in a database, I leave it up to the reader (whether the asker or someone else) to decide whether MSMQ is a technology they want to include in their solution. If the queue logic is already implemented in the database, then it may not make much sense to introduce MSMQ into the picture.
Dr. Wily's Apprentice
@Dr. Wiley's Apprentice: I'm unfamiliar with MSMQ (will research it shortly) but I can say that the queueing logic is implemented as a two-stage process. The first stage is assigning a priority which is stored in the DB, however priority isn't the only factor. A priority 1 object, may not be eligible for 5 years based on the stage two pass, which requires real-time information. When I received the codebase, the only queueing that was done was all stage two (on the entire dataset/table). Stage one (priority setting/checking) was implemented to cut down on the db thrashing.
SnOrfus
Let me be honest and say that MSMQ is not a technology that I work with very often, so you may want to take my suggestion with a grain of salt. It is a queuing technology, which is why it _might_ fit your needs. However, it's not intended for long-term storage, so if you're talking about having items sitting around for 5 years then you're likely going to want to keep your database for storage.
Dr. Wily's Apprentice
@Dr. W: I've used MSMQ. In general, I wouldn't be quick to recommend it. Based on the comments above, it doesn't seem to fit the specifics, either.
Steven Sudit