向自托管WCF服务添加可选的gzip压缩
如何为我的自托管WCF服务添加可选的gzip压缩?我正在为这个senario使用WebHttpBinding。我想检查Accept头是否包含字符串gzip,并压缩内容 我想使用自定义属性。到目前为止,我使用了一个自定义属性,它允许我在XML和JSON输出之间切换,但我刚才不知道如何压缩输出 在编码器开关属性中,我实现了IDispatchMessageFormatter接口,以按需更改XmlObjectSerializer。但是我不理解如何生成输出来修改它向自托管WCF服务添加可选的gzip压缩,wcf,rest,gzip,custom-attributes,Wcf,Rest,Gzip,Custom Attributes,如何为我的自托管WCF服务添加可选的gzip压缩?我正在为这个senario使用WebHttpBinding。我想检查Accept头是否包含字符串gzip,并压缩内容 我想使用自定义属性。到目前为止,我使用了一个自定义属性,它允许我在XML和JSON输出之间切换,但我刚才不知道如何压缩输出 在编码器开关属性中,我实现了IDispatchMessageFormatter接口,以按需更改XmlObjectSerializer。但是我不理解如何生成输出来修改它 如果有人能为我指出一个可能的解决方案,那
如果有人能为我指出一个可能的解决方案,那就太好了。这不是一个属性,但它是压缩WCF服务响应的基本代码,如果需要,可以包装成一个属性
public static void CompressResponseStream(HttpContext context = null)
{
if (context == null)
context = HttpContext.Current;
string encodings = context.Request.Headers.Get("Accept-Encoding");
if (!string.IsNullOrEmpty(encodings))
{
encodings = encodings.ToLowerInvariant();
if (encodings.Contains("deflate"))
{
context.Response.Filter = new DeflateStream(context.Response.Filter, CompressionMode.Compress);
context.Response.AppendHeader("Content-Encoding", "deflate");
context.Response.AppendHeader("X-CompressResponseStream", "deflate");
}
else if (encodings.Contains("gzip"))
{
context.Response.Filter = new GZipStream(context.Response.Filter, CompressionMode.Compress);
context.Response.AppendHeader("Content-Encoding", "gzip");
context.Response.AppendHeader("X-CompressResponseStream", "gzip");
}
else
{
context.Response.AppendHeader("X-CompressResponseStream", "no-known-accept");
}
}
}
[编辑]发表评论:
只需在web服务操作主体中的任意位置调用它,因为它会在响应上设置属性:
[OperationContract]
public ReturnType GetInformation(...) {
// do some stuff
CompressResponseStream();
}
这不是一个属性,但它是压缩WCF服务响应的基本代码,如果需要,可以将其包装到属性中
public static void CompressResponseStream(HttpContext context = null)
{
if (context == null)
context = HttpContext.Current;
string encodings = context.Request.Headers.Get("Accept-Encoding");
if (!string.IsNullOrEmpty(encodings))
{
encodings = encodings.ToLowerInvariant();
if (encodings.Contains("deflate"))
{
context.Response.Filter = new DeflateStream(context.Response.Filter, CompressionMode.Compress);
context.Response.AppendHeader("Content-Encoding", "deflate");
context.Response.AppendHeader("X-CompressResponseStream", "deflate");
}
else if (encodings.Contains("gzip"))
{
context.Response.Filter = new GZipStream(context.Response.Filter, CompressionMode.Compress);
context.Response.AppendHeader("Content-Encoding", "gzip");
context.Response.AppendHeader("X-CompressResponseStream", "gzip");
}
else
{
context.Response.AppendHeader("X-CompressResponseStream", "no-known-accept");
}
}
}
[编辑]发表评论:
只需在web服务操作主体中的任意位置调用它,因为它会在响应上设置属性:
[OperationContract]
public ReturnType GetInformation(...) {
// do some stuff
CompressResponseStream();
}
有趣的问题。为什么它必须是可选的?只是好奇。我认为gzip压缩是几乎所有使用http的设备都支持的标准,所以我的托管WCF服务是在IIS配置级别进行的。这会很好,这样就更容易通过telnet测试服务。另外,我不确定第三方客户机处理压缩是否容易。啊,您可能想尝试Fiddler来测试您的WCF http服务。太棒了。我还没有遇到过一款不自动解压缩gzip的设备,但我想我不能肯定是否所有的第三方客户端都能处理它。当然,你仍然有一个完全正确的问题。我只是觉得如果你能避免的话,这可能会节省你一些时间。有趣的问题。为什么它必须是可选的?只是好奇。我认为gzip压缩是几乎所有使用http的设备都支持的标准,所以我的托管WCF服务是在IIS配置级别进行的。这会很好,这样就更容易通过telnet测试服务。另外,我不确定第三方客户机处理压缩是否容易。啊,您可能想尝试Fiddler来测试您的WCF http服务。太棒了。我还没有遇到过一款不自动解压缩gzip的设备,但我想我不能肯定是否所有的第三方客户端都能处理它。当然,你仍然有一个完全正确的问题。我只是想如果你能避免的话,可能会节省你一些时间。我放弃了那个项目,但看起来真的很好。如果你能提供一个调用函数的例子,我会接受你的答案。我放弃了那个项目,但看起来真的很好。如果你能提供一个调用函数的例子,我会接受你的答案。