You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If you want to change the default binding ServiceStack uses, you can register your own Custom Request Binder.
Custom Media Types
ServiceStack serializes and deserializes your DTOs automatically. If you want to override the default serializers or you want to add a new format, you have to register your own Content-Type:
Register a custom format
stringcontentType="application/yourformat";//To override JSON eg, write "application/json"varserialize=(IRequestreq,objectresponse,Streamstream)=> ...;vardeserialize=(Typetype,Streamstream)=> ...;//In AppHost Configure method//Pass two delegates for serialization and deserializationthis.ContentTypes.Register(contentType,serialize,deserialize);
The Protobuf-format shows an example of registering a new format whilst the Northwind VCard Format shows an example of creating a custom media type in ServiceStack.
Reading in and De-Serializing ad-hoc custom requests
There are 2 ways to deserialize your own custom format, via attaching a custom request binder for a particular service or marking your service with IRequiresRequestStream which will skip auto-deserialization and inject the ASP.NET Request stream instead.
Create a custom request dto binder
You can register custom binders in your AppHost by using the example below:
This gives you access to the IHttpRequest object letting you parse it manually so you can construct and return the strong-typed request DTO manually which will be passed to the service instead.
Uploading Files
You can access uploaded files independently of the Request DTO using Request.Files. e.g:
Instead of registering a custom binder you can skip the serialization of the request DTO, you can add the IRequiresRequestStream interface to directly retrieve the stream without populating the request DTO.
//Request DTOpublicclassHello:IRequiresRequestStream{/// <summary>/// The raw Http Request Input Stream/// </summary>StreamRequestStream{get;set;}}
You can access raw WCF Message when accessed with the SOAP endpoints in your Service with IHttpRequest.GetSoapMessage() extension method, e.g:
To tell ServiceStack to skip Deserializing the SOAP request entirely, add the IRequiresSoapMessage interface to your Request DTO, e.g:
public class RawWcfMessage : IRequiresSoapMessage {
public Message Message { get; set; }
}
public object Post(RawWcfMessage request) {
request.Message... //Raw WCF SOAP Message
}
Buffering the Request and Response Streams
ServiceStack's Request and Response stream are non-buffered (i.e. forward-only) by default. This can be changed at runtime using a PreRequestFilter to allow the Request Body and Response Output stream to be re-read multiple times should your Services need it: