Service return types
This page has moved to docs.servicestack.net/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
The following types are not converted (to different Content-Types) but get written directly to the Response Stream:
String
Stream
IStreamWriter
-
byte[]
- with theapplication/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;
}
}
}
- /hello/ServiceStack
- /hellotext/ServiceStack
- /hellohtml/ServiceStack
- /helloimage/ServiceStack?Width=600&height=300&Foreground=Yellow
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:
return new HttpResult(new FileInfo(filePath), request.MimeType);
return new HttpResult(ms, "audio/mpeg");
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.
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.
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.
See the Customize HTTP Responses wiki page for more ways of customizing the HTTP Response.
- Why ServiceStack?
- Important role of DTOs
- What is a message based web service?
- Advantages of message based web services
- Why remote services should use separate DTOs
-
Getting Started
-
Designing APIs
-
Reference
-
Clients
-
Formats
-
View Engines 4. Razor & Markdown Razor
-
Hosts
-
Security
-
Advanced
- Configuration options
- Access HTTP specific features in services
- Logging
- Serialization/deserialization
- Request/response filters
- Filter attributes
- Concurrency Model
- Built-in profiling
- Form Hijacking Prevention
- Auto-Mapping
- HTTP Utils
- Dump Utils
- Virtual File System
- Config API
- Physical Project Structure
- Modularizing Services
- MVC Integration
- ServiceStack Integration
- Embedded Native Desktop Apps
- Auto Batched Requests
- Versioning
- Multitenancy
-
Caching
-
HTTP Caching 1. CacheResponse Attribute 2. Cache Aware Clients
-
Auto Query
-
AutoQuery Data 1. AutoQuery Memory 2. AutoQuery Service 3. AutoQuery DynamoDB
-
Server Events
-
Service Gateway
-
Encrypted Messaging
-
Plugins
-
Tests
-
ServiceStackVS
-
Other Languages
-
Amazon Web Services
-
Deployment
-
Install 3rd Party Products
-
Use Cases
-
Performance
-
Other Products
-
Future