Edit on GitHub

Service Return Types

From a birds-eye view ServiceStack can return any of:

  • Any DTO object -> serialized to Response ContentType
  • HttpResult, HttpError, CompressedResult (IHttpResult) for Customized HTTP response

Different Return Types

The following types are not converted (to different Content-Types) but get written directly to the Response Stream:

  • String
  • Stream
  • IStreamWriter
  • byte[] - with the application/octet-stream Content Type.

From the HelloWorld ServiceStack.UseCase demo:

public class HelloService : Service
{
    public HelloResponse Get(Hello request)
    {
        return new HelloResponse { Result = "Hello, {0}!".Fmt(request.Name) };

        //C# client can call with:
        //var response = client.Get(new Hello { Name = "ServiceStack" });
    }

    public string Get(HelloHtml request)
    {
        return "<h1>Hello, {0}!</h1>".Fmt(request.Name);
    }

    [AddHeader(ContentType = "text/plain")]
    public string Get(HelloText request)
    {
        return "<h1>Hello, {0}!</h1>".Fmt(request.Name);
    }

    [AddHeader(ContentType = "image/png")]
    public Stream Get(HelloImage request)
    {
        var width = request.Width.GetValueOrDefault(640);
        var height = request.Height.GetValueOrDefault(360);
        var bgColor = request.Background != null ? Color.FromName(request.Background) : Color.ForestGreen;
        var fgColor = request.Foreground != null ? Color.FromName(request.Foreground) : Color.White;

        var image = new Bitmap(width, height);
        using (var g = Graphics.FromImage(image))
        {
            g.Clear(bgColor);

            var drawString = "Hello, {0}!".Fmt(request.Name);
            var drawFont = new Font("Times", request.FontSize.GetValueOrDefault(40));
            var drawBrush = new SolidBrush(fgColor);
            var drawRect = new RectangleF(0, 0, width, height);

            var drawFormat = new StringFormat {
                LineAlignment = StringAlignment.Center,
                Alignment = StringAlignment.Center };

            g.DrawString(drawString, drawFont, drawBrush, drawRect, drawFormat);

            var ms = new MemoryStream();
            image.Save(ms, ImageFormat.Png);
            return ms;
        }
    }
}

Live Examples of the above Hello Service:

Partial Content Support

Partial Content Support allows a resource to be split up an accessed in multiple chunks for clients that support HTTP Range Requests. This is a popular feature in download managers for resuming downloads of large files and streaming services for real-time streaming of content (e.g. consumed whilst it’s being watched or listened to).

HTTP Partial Content Support is added in true ServiceStack-style where it’s now automatically and transparently enabled for any existing services returning:

A Physical File

return new HttpResult(new FileInfo(filePath), request.MimeType); 

A Memory Stream

return new HttpResult(ms, "audio/mpeg");

Raw Text

return new HttpResult(customText, "text/plain");

Partial Content was also added to static file downloads served directly through ServiceStack which lets you stream mp3 downloads or should you ever want to your static .html, .css, .js, etc.

You can disable Partial Content support with Config.AllowPartialResponses = false;.

See the PartialContentResultTests for more examples.

Writing directly to the Response Stream

In addition to returning plain C# objects, ServiceStack allows you to return any Stream or IStreamWriter (which is a bit more flexible on how you write to the response stream):

public interface IStreamWriter
{
    void WriteTo(Stream stream);
}

Both though allow you to write directly to the Response OutputStream without any additional conversion overhead.

Customizing HTTP Headers

If you want to customize the HTTP headers at the sametime you just need to implement IHasOptions where any Dictionary Entry is written to the Response HttpHeaders.

public interface IHasOptions
{
    IDictionary<string, string> Options { get; }
}

Further than that, the IHttpResult allows even finer-grain control of the HTTP output (status code, headers, …) where you can supply a custom Http Response status code. You can refer to the implementation of the HttpResult object for a real-world implementation of these above interfaces.

Further customizing the HTTP Response

See the Customize HTTP Responses page for more ways of customizing the HTTP Response.