ServiceStack 是否支持二进制响应? [英] Does ServiceStack support binary responses?

查看:37
本文介绍了ServiceStack 是否支持二进制响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ServiceStack 服务中是否有任何机制来返回流/大二进制数据?WCF 的 MTOM 支持虽然笨拙,但在返回大量数据而没有文本转换开销方面很有效.

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 without text conversion overhead.

推荐答案

鸟瞰 ServiceStack 可以返回任何一个:

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

  • 任何 DTO 对象 -> 序列化为响应 ContentType
  • 用于自定义 HTTP 响应的 HttpResult、HttpError、CompressedResult (IHttpResult)

以下类型未转换并直接写入响应流:

The following types are not converted and get written directly to the Response Stream:

  • 字符串
  • IStreamWriter
  • byte[] - 带有 application/octet-stream 内容类型.
  • String
  • Stream
  • IStreamWriter
  • byte[] - with the application/octet-stream Content Type.

除了返回普通的 C# 对象之外,ServiceStack 还允许您返回任何 Stream 或 IStreamWriter(这在您写入响应流的方式上更加灵活):

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);
}

两者都允许您直接写入 Response OutputStream 而无需任何额外的转换开销.

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

如果您想同时自定义 HTTP 标头,您只需要实现 IHasOptions,其中任何字典条目都写入响应 HttpHeaders.

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; }
}

此外,IHttpResult 允许对 HTTP 输出进行更细粒度的控制,您可以在其中提供自定义 Http 响应状态代码.可以参考HttpResult 用于实际实现上述接口的对象.

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.

这篇关于ServiceStack 是否支持二进制响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆