Send a large message to a ServiceStack service

2019-04-13 13:20发布

问题:

I need to create a service that will allow a client to send a message containing a large amount of data and I'm not sure how to structure the API.

Let's say a client wants to save a new object which contains a variable number of related objects. For instance, an Order, which contains several line items contained in OrderDetail objects. An Order may have over a 1000 OrderDetail objects related to it, each of which may contain 20-40KB of data. The client needs to know that the service has received the entire order.

I would like to explore using ServiceStack to create this. Creating a high traffic service is not something I'm very familiar with. We would typically use WCF, and it seems like people just recommend increasing the message size limit to accommodate a large message. I'm not sure if that's the best strategy with WCF, let alone ServiceStack.

Would it be better to send each OrderDetail as it's own message? I would be concerned about keeping the integrity of the entire Order going this route. Or should I just keep this as a single large message 20-40MB of data? Or should I try to stream it as a file?

回答1:

Use ServiceStack's ProtoBuf support - protocol buffers is the most efficient and compact wire format out there. Since it's simpler, try that first, before investigating the Streaming options in ServiceStack.

Streaming options in ServiceStack

If you've identified that your service will greatly benefit from Streaming, than here's an article on sending a stream to ServiceStack that shows how to Stream inside ServiceStack services. It shows how to use IRequiresRequestStream which lets you stream the Request Body in your services:

Request DTO:

[Route("/upload/{FileName}", "POST")]
public class UploadPackage : IRequiresRequestStream
{
    public System.IO.Stream RequestStream { get; set; }

    public string FileName { get; set; }
}

Access to the Request Body Stream is injected into the RequestStream property of the Request DTO.