Building a Queue Service - Serializing Tasks for later use
I am working on creating a “Queue Manager” service and its sole responsibility is to add work items to the queue and remove work items from the queue. I have implemented the service as a singleton because I have some unique synchronization issues I have to deal with. Any time a new task is needed, I create a new Task object, set the parameters and then send it to the QueueManager service and have it added to a SQL Database. The Task object is serialized to the database at that time. When a process requests an item from the QueueManager, the service finds the next available work item and deserializes it back to a Task object – from there it gets returned to the client.
Technically, this works great. However, I’m not happy with having to shoehorn all types of tasks into a Task object. Work items can range from anything from copying files to updating databases. I have to add a lot of properties to the Task object that don’t mean anything to many other task types – it’s getting pretty sloppy. My first thought on how to fix this would be to have Task implemented as a base class, and then have all other tasks derive from Task. This would allow me to have work items such as CopyFileTask, UpdateDatabaseTask, DoLaundryTask, etc. I would then serialize the object to the database and add a column to tell me what the type is so I know how to deserialize it later.
The first problem I see doing it this way would be that the WCF Service needs to return just one type. That would require the service to down cast the work item (CopyFileTask for example) to Task. However, I now need to cast it back to its real type, but the client will have no idea what that type is.
I’m probably not doing a good job explaining this. In a nutshell, I need to:
- Create a work object of any type
- Pass it to a WCF service to have it stored in a DB
- Call a WCF service to get a new work item (anything it finds)
- Have that work item retuned to the client as a real (non casted) object
My Queue needs to be very generic since many other applications are adding and removing work items. This is being designed for a massive parallel system and will be the hub for all distributed work items. Any and all help and ideas will be greatly appriciated.
Have you looked at ServiceKnownTypeAttribute? Have a look at the sample code on this MSDN: http://msdn.microsoft.com/en-us/library/system.servicemodel.serviceknowntypeattribute.aspx