Json 如何在Windows Azure动态内容上启用gzip HTTP压缩

Json 如何在Windows Azure动态内容上启用gzip HTTP压缩,json,gzip,azure,http-compression,Json,Gzip,Azure,Http Compression,我一直试图在我的WindowsAzure托管的WCF Restful服务上启用gzip HTTP压缩,该服务仅从GET和POST请求返回JSON,但未成功 我已经尝试了很多东西,所以我很难列出所有这些东西,现在我意识到我一直在处理相互冲突的信息(关于azure的旧版本等),所以我认为最好从头开始 我正在使用VisualStudio2008,使用VisualStudio的2010年2月工具 因此,根据以下内容 。。HTTP压缩现在已启用。我在下一页使用了建议(仅URL压缩建议) 。。但是我没

我一直试图在我的WindowsAzure托管的WCF Restful服务上启用gzip HTTP压缩,该服务仅从GET和POST请求返回JSON,但未成功

我已经尝试了很多东西,所以我很难列出所有这些东西,现在我意识到我一直在处理相互冲突的信息(关于azure的旧版本等),所以我认为最好从头开始

我正在使用VisualStudio2008,使用VisualStudio的2010年2月工具

因此,根据以下内容

。。HTTP压缩现在已启用。我在下一页使用了建议(仅URL压缩建议)


。。但是我没有压缩。我不知道urlCompressionhttpCompression之间有什么区别,这对我没有帮助。我已经试过了,但是没有用

VisualStudio工具是在支持压缩的Azure版本之前发布的,这会是一个问题吗?我在某个地方读到,使用最新的工具,您可以选择发布时要使用的Azure OS版本。。。但我不知道这是不是真的,如果是真的,我也找不到可以选择的地方。我可以使用启用http之前的版本吗

我还尝试了blowery http压缩模块,但没有结果

有没有人对如何实现这一目标有什么最新的建议?i、 e.与Azure操作系统当前版本相关的建议

干杯

史蒂文

更新:我编辑了上述代码以修复web.config代码段中的一个类型

更新2:使用下面的答案中显示的whatsmyip URL测试响应表明,从my service.svc返回的JSON响应没有任何压缩,但是返回的静态HTML页面带有gzip压缩。关于如何压缩JSON响应的任何建议都将不胜感激


更新3:尝试了一个大于256KB的JSON响应,以查看问题是否是由于JSON响应小于此值所致,如下面的注释所述。不幸的是,响应仍然未压缩。

是的,您可以选择所需的操作系统,但默认情况下,您将获得最新的操作系统

压缩很棘手。有很多事情会出错。您是否碰巧在代理服务器后面进行此测试?我相信IIS默认情况下不会向代理发送压缩内容。我发现了一个方便的工具来测试压缩是否工作时,我正在玩这个:


看起来您有了doDynamicCompression=“false”。。。那只是打字错误吗?如果要对从web服务返回的JSON进行压缩,您希望启用该功能。

至少我不是唯一一个这样做的人,而且几乎一年后,这仍然是一个愚蠢的PITA

问题是MIME类型不匹配。WCF返回内容类型为application/JSON的JSON响应;字符集=UTF-8。在该页面的中间部分,没有将其作为可压缩MIME类型包含

现在,在web.config中添加一个
部分,并将application/json添加到该部分可能会很诱人。但这只是浪费一两个小时的一种不好的方式——您只能在applicationHost.config级别更改
元素

因此,有两种可能的解决方案。首先,您可以将WCF响应更改为使用默认配置中可压缩的MIME类型
text/json
将起作用,因此将其添加到您的服务方法将为您提供动态压缩:
WebOperationContext.Current.OutgoingResponse.ContentType=“text/json”

或者,您可以使用appcmd和启动任务更改applicationHost.config文件。这一点(除其他事项外)将在上一节中讨论。请注意,如果添加该启动任务并在dev结构中运行它,它将只工作一次。第二次将失败,因为您已经添加了配置元素。我最终用一个单独的csdef文件创建了第二个云项目,这样我的devfabric就不会运行那个启动脚本。不过,可能还有其他解决方案

更新
我在前一段中提出的单独项目的建议并不是一个好主意。非幂等启动任务是一个非常糟糕的主意,因为有一天Azure fabric将决定为您重新启动角色,启动任务将失败,并进入循环。很可能在深夜。相反,将启动任务设为幂等,如中所述

这花了很长时间。。。但我终于解决了这个问题,我想为其他任何挣扎的人发布答案。这个解决方案非常简单,我已经证实它确实有效

编辑ServiceDefinition.csdef文件,将其包含在WebRole标记中:

    <Startup>
      <Task commandLine="EnableCompression.cmd" executionContext="elevated" taskType="simple"></Task>
    </Startup>

。。就这样!完成!这为web角色返回的json启用了动态压缩,我想我在某处读到了一个相当奇怪的mime类型,因此请确保准确复制代码。

为了处理首次部署后出现问题的本地开发结构,我在CMD文件中添加了相应的命令以重置配置。此外,我在这里特别设置了压缩级别,因为在某些(所有?)情况下它似乎默认为零


MS的这篇文章是他们如何编写JSON脚本的


它处理了提到的许多问题,例如能够处理Azure回收等

刚刚遇到了一个关于错误类型183的问题,我找到了一个解决方案。因此,如果其他任何人正在经历这种情况,请执行以下操作:

以下是我得到的错误:

用户程序“F:\approt\bin\EnableCompression.cmd”已退出,退出代码为183,非零。工作目录为F:\Approt\bin

下面是为我修改的代码:

REM   *** Add a compression section to the Web.config file. ***
%windir%\system32\inetsrv\appcmd set config /section:urlCompression /doDynamicCompression:True /commit:apphost >> "%TEMP%\StartupLog.txt" 2>&1

REM   ERRORLEVEL 183 occurs when trying to add a section that already exists. This error is expected if this
REM   batch file were executed twice. This can occur and must be accounted for in a Windows Azure startup
REM   task. To handle this situation, set the ERRORLEVEL to zero by using the Verify command. The Verify
REM   command will safely set the ERRORLEVEL to zero.
IF %ERRORLEVEL% EQU 183 DO VERIFY > NUL

REM   If the ERRORLEVEL is not zero at this point, some other error occurred.
IF %ERRORLEVEL% NEQ 0 (
   ECHO Error adding a compression section to the Web.config file. >> "%TEMP%\StartupLog.txt" 2>&1
   GOTO ErrorExit
)

REM   *** Add compression for json. ***
%windir%\system32\inetsrv\appcmd set config  -section:system.webServer/httpCompression /+"dynamicTypes.[mimeType='application/json; charset=utf-8',enabled='True']" /commit:apphost >> "%TEMP%\StartupLog.txt" 2>&1
IF %ERRORLEVEL% EQU 183 VERIFY > NUL
IF %ERRORLEVEL% NEQ 0 (
   ECHO Error adding the JSON compression type to the Web.config file. >> "%TEMP%\StartupLog.txt" 2>&1
   GOTO ErrorExit
)

REM   *** Exit batch file. ***
EXIT /b 0

REM   *** Log error and exit ***
:ErrorExit
REM   Report the date, time, and ERRORLEVEL of the error.
DATE /T >> "%TEMP%\StartupLog.txt" 2>&1
TIME /T >> "%TEMP%\StartupLog.txt" 2>&1
ECHO An error occurred during startup. ERRORLEVEL = %ERRORLEVEL% >> "%TEMP%\StartupLog.txt" 2>&1
EXIT %ERRORLEVEL%

找到解决方案嘿!谢谢你的快速回复!这是一个打字错误,这行应该是:
%windir%\system32\inetsrv\appcmd set config /section:urlCompression /doDynamicCompression:True /commit:apphost
%windir%\system32\inetsrv\appcmd set config  -section:system.webServer/httpCompression /+"dynamicTypes.[mimeType='application/json; charset=utf-8',enabled='True']" /commit:apphost
REM Remove old settings - keeps local deploys working (since you get errors otherwise)
%windir%\system32\inetsrv\appcmd reset config -section:urlCompression
%windir%\system32\inetsrv\appcmd reset config -section:system.webServer/httpCompression 

REM urlCompression - is this needed?
%windir%\system32\inetsrv\appcmd set config -section:urlCompression /doDynamicCompression:True /commit:apphost
REM Enable json mime type
%windir%\system32\inetsrv\appcmd set config -section:system.webServer/httpCompression /+"dynamicTypes.[mimeType='application/json; charset=utf-8',enabled='True']" /commit:apphost

REM IIS Defaults
%windir%\system32\inetsrv\appcmd set config -section:system.webServer/httpCompression /+"dynamicTypes.[mimeType='text/*',enabled='True']" /commit:apphost
%windir%\system32\inetsrv\appcmd set config -section:system.webServer/httpCompression /+"dynamicTypes.[mimeType='message/*',enabled='True']" /commit:apphost
%windir%\system32\inetsrv\appcmd set config -section:system.webServer/httpCompression /+"dynamicTypes.[mimeType='application/x-javascript',enabled='True']" /commit:apphost
%windir%\system32\inetsrv\appcmd set config -section:system.webServer/httpCompression /+"dynamicTypes.[mimeType='*/*',enabled='False']" /commit:apphost
%windir%\system32\inetsrv\appcmd set config -section:system.webServer/httpCompression /+"staticTypes.[mimeType='text/*',enabled='True']" /commit:apphost
%windir%\system32\inetsrv\appcmd set config -section:system.webServer/httpCompression /+"staticTypes.[mimeType='message/*',enabled='True']" /commit:apphost
%windir%\system32\inetsrv\appcmd set config -section:system.webServer/httpCompression /+"staticTypes.[mimeType='application/javascript',enabled='True']" /commit:apphost
%windir%\system32\inetsrv\appcmd set config -section:system.webServer/httpCompression /+"staticTypes.[mimeType='*/*',enabled='False']" /commit:apphost

REM Set dynamic compression level to appropriate level.  Note gzip will already be present because of reset above, but compression level will be zero after reset.
%windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/httpCompression /+"[name='deflate',doStaticCompression='True',doDynamicCompression='True',dynamicCompressionLevel='7',dll='%%Windir%%\system32\inetsrv\gzip.dll']" /commit:apphost
%windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/httpCompression -[name='gzip'].dynamicCompressionLevel:7 /commit:apphost
REM   *** Add a compression section to the Web.config file. ***
%windir%\system32\inetsrv\appcmd set config /section:urlCompression /doDynamicCompression:True /commit:apphost >> "%TEMP%\StartupLog.txt" 2>&1

REM   ERRORLEVEL 183 occurs when trying to add a section that already exists. This error is expected if this
REM   batch file were executed twice. This can occur and must be accounted for in a Windows Azure startup
REM   task. To handle this situation, set the ERRORLEVEL to zero by using the Verify command. The Verify
REM   command will safely set the ERRORLEVEL to zero.
IF %ERRORLEVEL% EQU 183 DO VERIFY > NUL

REM   If the ERRORLEVEL is not zero at this point, some other error occurred.
IF %ERRORLEVEL% NEQ 0 (
   ECHO Error adding a compression section to the Web.config file. >> "%TEMP%\StartupLog.txt" 2>&1
   GOTO ErrorExit
)

REM   *** Add compression for json. ***
%windir%\system32\inetsrv\appcmd set config  -section:system.webServer/httpCompression /+"dynamicTypes.[mimeType='application/json; charset=utf-8',enabled='True']" /commit:apphost >> "%TEMP%\StartupLog.txt" 2>&1
IF %ERRORLEVEL% EQU 183 VERIFY > NUL
IF %ERRORLEVEL% NEQ 0 (
   ECHO Error adding the JSON compression type to the Web.config file. >> "%TEMP%\StartupLog.txt" 2>&1
   GOTO ErrorExit
)

REM   *** Exit batch file. ***
EXIT /b 0

REM   *** Log error and exit ***
:ErrorExit
REM   Report the date, time, and ERRORLEVEL of the error.
DATE /T >> "%TEMP%\StartupLog.txt" 2>&1
TIME /T >> "%TEMP%\StartupLog.txt" 2>&1
ECHO An error occurred during startup. ERRORLEVEL = %ERRORLEVEL% >> "%TEMP%\StartupLog.txt" 2>&1
EXIT %ERRORLEVEL%