Does ServiceStack support binary responses?

后端 未结 3 2207
[愿得一人]
[愿得一人] 2020-11-28 11:06

Is there any mechanism in ServiceStack services to return streaming/large binary data? WCF\'s MTOM support is awkward but effective in returning large amounts of data withou

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-28 11:23

    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 and get written directly to the Response Stream:

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

    Details

    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 Options { get; }
    }
    

    Further than that, the IHttpResult allows even finer-grain control of the HTTP output 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.

提交回复
热议问题