1

I have a working REST API using ServiceStack. I then needed several of my routes and DTO to be more realtime and persist. So I added websockets outside of the REST API and used ServiceStack.Text for DTO serialization / deserialization. That worked great, but then I wanted to have more then one DTO type. That caused a problem, because I need to pass the JSON content as well as DTO type over the websocket.

In all my searching, I have not found an integration of websockets into ServiceStack, and I probably could not adopt it anyway, as I've got to use an older ServiceStack that is only dependent on .NET 3.5. So far this is what I've done. I've used SuperSocket for the client side of the websocket. I've used Alchemy for the server. And I've used ServiceStack to serialize / deserialize my single DTO type. Any other coding approach ideas out there to allow multiple DTO types over websocket?

Here is one idea I'm toying with. Have all DTOs have a string field that is the name of the DTO, called "DTO". Then when I get a websocket request, I do a switch statement on the msg.DTO field, and deserialize msg for that DTO type. Generally my DTO types map to Redis typed values, so I'm usually looking to send a Redis key and get back a strongly typed DTO response.

I also could send a full route like it was to the REST API, but send the route as a string over the websocket. Then I could just invoke the REST API from within, which does not have the cost of an external request? But I still have to know the DTO so I can serialize properly.

So with both cases if the first field in all my DTO's was a string of the name of the DTO, then I can parse it out without deserializing. Once I have the DTO name, then I can deserialize to the DTO.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.