Ian Kaney - 3 - Feb 23, 2015

Currently working on a database abstraction layer using ServiceStack for REST calls between the client and server. When I’m testing with large file uploads to the database (circa 60MB) I’m getting out of memory exceptions when the client is generating the data for the ‘post’ request.

I’ve tried changing over to MsgPack format but this broke everything for some strange reason even though it’s meant to be interchangeable.

Is it possible what I’m trying, or should I give up and try a different approach?

ServiceStack doesn’t have any built-in limits itself, but it’s subject to the ASP.NET Request Limits which you can increase in your Web.config with: http://stackoverflow.com/a/3853785/85785

Ian Kaney:

I’m using JsonServiceClient and I’m getting out of memory exceptions when it’s performing the Post with the binary data in there. From the stack trace it seems to be using convert.base64 routines which do seem to fall over with large files?

Oh you’re trying to send a base64-encoded binary file with JSON? that’s not going to be very efficient. You should either use the PostFile API on ServiceClient: https://github.com/ServiceStack/ServiceStack/blob/master/tests/ServiceStack.WebHost.Endpoints.Tests/FileUploadTests.cs or PostFileToUrl() or PostBytesToUrl() in HTTP Utils: https://github.com/ServiceStack/ServiceStack/wiki/Http-Utils#uploading-files

Ian Kaney:

Brilliant, I’ll give that a go and see if it helps when uploading the file.

Out of interest, what would be the best way for me to get the file data from the database and send it back to the client without hitting out of memory exceptions? Would this be where one of the binary protocols would assist?

There are a few options mentioned in this answer: http://stackoverflow.com/a/28540206/85785 The test ImageServices shows different ways to return an image at: https://github.com/ServiceStack/Test/blob/master/src/Test/Test.ServiceInterface/ImageService.cs
The streaming options that will allow you to stream a file without holding the entire contents in memory include:  returning a Stream, returning a decorated Stream in HttpResult(stream), returning a custom IStreamWriter or writing directly to the Response.OutputStream.