none
How to compress WCF 3.5 response & use WCF service in WPF application RRS feed

  • Question

  •       I want to send compress data from WCF 3.5 service as a response & access that responce in WPF application. I have hosted WCF application on IIS & implemented IIS Compression to send compress response to client. I have implemented iis compression by referring following link.
         http://www.hanselman.com   /blog/EnablingDynamicCompressionGzipDeflateForWCFDataFeedsODataAndOtherCustomServicesInIIS7.aspx
          Compression work properly when I access that WCF service in MVC application. But when I try to access same WCF service in WPF application Fiddler showing that service response is not compressed.
         Please help me to solve this problem.
    • Edited by Sagart04 Thursday, June 6, 2013 10:18 AM
    Thursday, June 6, 2013 10:16 AM

Answers

  • I assume the MVC headers are showing gzip.  I think you need to add these settings below in your config file.  see this webpage for lots of usuful info

    http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/d9eb22c4-0012-4496-8e58-ff32d68442ba

    <system.net>
      <webRequestModules>
        <remove prefix="http:"/>
        <add prefix="http:" type="Pajocomo.Net.CompressibleHttpRequestCreator, CompressibleHTTP" />
        <remove prefix="https:"/>
        <add prefix="https:" type="Pajocomo.Net.CompressibleHttpRequestCreator, CompressibleHTTP" />
      </webRequestModules>
    </system.net>


    jdweng

    • Marked as answer by Sagart04 Monday, June 17, 2013 11:29 AM
    Thursday, June 6, 2013 10:47 AM
  • I usually use wireshark and not fiddler so I not familar with the the wasy fiddler displays data.  Wireshark will automatically show the deflated gzip data and not the packed gzip format.  I don't know if fiddler does the same.  Look at the actual data packets and see if you get the correct response.  It appears everything is working properly.  I think the only reason you think the data isn't compressinmg is because fiddler is automatically decompressing the data.  The response header indicates the message was using gzip.

    There are three things that I notice in the header you posted that is important

    1) The data is HTTP/1.1 (not 1.0).  This probably means the data is being sent in chunk mode.

    2) The response status is "200 OK" which means you got all the data.

    3) The content length is 145639


    jdweng

    Friday, June 7, 2013 8:52 AM
  •       I have added client side code which you have suggested, by referring following link

    http://www.codeproject.com/Articles/12698/HTTP-compression-in-NET-Framework-2-0

       also i have run following command in command promptby using following post

    http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/d9eb22c4-0012-4496-8e58-ff32d68442ba

    cscript.exe adsutil.vbs set W3Svc/Filters/Compression/GZIP/HcScriptFileExtensions "asp" "dll" "exe" "svc"

    cscript.exe adsutil.vbs set W3Svc/Filters/Compression/GZIP/HcDynamicCompressionLevel 9

          I try following code in wcf to add compression in httpResponse as sujjested in following post

    http://social.msdn.microsoft.com/forums/en-US/wcf/thread/ddfe06b1-f07c-4da2-a1f1-d06126e4f96e/

      
       string acceptEncoding = HttpContext.Current.Request.Headers["Accept-Encoding"];
    if (!string.IsNullOrEmpty(acceptEncoding) && acceptEncoding.IndexOf("gzip", StringComparison.OrdinalIgnoreCase) > -1)
    {
      HttpContext.Current.Response.Filter = new GZipStream(
        HttpContext.Current.Response.Filter,
        CompressionMode.Compress);
      HttpContext.Current.Response.AppendHeader("Content-Encoding", "gzip");
    }

        Then also wcf response is not compressing. I got following information in fidder

    Request Header

    POST /LiteServicetInternet/Demo/DealMemoService.svc HTTP/1.1
    Content-Type: text/xml; charset=utf-8
    VsDebuggerCausalityData: uIDPo29XgKxCkCRClgS1QfX7j6gAAAAAtpYJMTHF4UeGDojLOIHqF/v2tRgMHXdFtIe/6f5yjskACQAA
    SOAPAction: "http://tempuri.org/IDealMemo/SearchCompetitorForLookUp"
    Host: ntvmsynrpt0389
    Content-Length: 819
    Expect: 100-continue
    Accept-Encoding: gzip, deflate


      This work for me that WCF service now sending compressed data.
    Monday, June 17, 2013 11:28 AM

All replies

  • I assume the MVC headers are showing gzip.  I think you need to add these settings below in your config file.  see this webpage for lots of usuful info

    http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/d9eb22c4-0012-4496-8e58-ff32d68442ba

    <system.net>
      <webRequestModules>
        <remove prefix="http:"/>
        <add prefix="http:" type="Pajocomo.Net.CompressibleHttpRequestCreator, CompressibleHTTP" />
        <remove prefix="https:"/>
        <add prefix="https:" type="Pajocomo.Net.CompressibleHttpRequestCreator, CompressibleHTTP" />
      </webRequestModules>
    </system.net>


    jdweng

    • Marked as answer by Sagart04 Monday, June 17, 2013 11:29 AM
    Thursday, June 6, 2013 10:47 AM
  •       I have added client side code which you have suggested, by referring following link

    http://www.codeproject.com/Articles/12698/HTTP-compression-in-NET-Framework-2-0

       also i have run following command in command promptby using following post

    http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/d9eb22c4-0012-4496-8e58-ff32d68442ba

    cscript.exe adsutil.vbs set W3Svc/Filters/Compression/GZIP/HcScriptFileExtensions "asp" "dll" "exe" "svc"

    cscript.exe adsutil.vbs set W3Svc/Filters/Compression/GZIP/HcDynamicCompressionLevel 9

          I try following code in wcf to add compression in httpResponse as sujjested in following post

    http://social.msdn.microsoft.com/forums/en-US/wcf/thread/ddfe06b1-f07c-4da2-a1f1-d06126e4f96e/

      
       string acceptEncoding = HttpContext.Current.Request.Headers["Accept-Encoding"];
    if (!string.IsNullOrEmpty(acceptEncoding) && acceptEncoding.IndexOf("gzip", StringComparison.OrdinalIgnoreCase) > -1)
    {
      HttpContext.Current.Response.Filter = new GZipStream(
        HttpContext.Current.Response.Filter,
        CompressionMode.Compress);
      HttpContext.Current.Response.AppendHeader("Content-Encoding", "gzip");
    }

        Then also wcf response is not compressing. I got following information in fidder

    Request Header

    POST /LiteServicetInternet/Demo/DealMemoService.svc HTTP/1.1
    Content-Type: text/xml; charset=utf-8
    VsDebuggerCausalityData: uIDPo29XgKxCkCRClgS1QfX7j6gAAAAAtpYJMTHF4UeGDojLOIHqF/v2tRgMHXdFtIe/6f5yjskACQAA
    SOAPAction: "http://tempuri.org/IDealMemo/SearchCompetitorForLookUp"
    Host: ntvmsynrpt0389
    Content-Length: 819
    Expect: 100-continue
    Accept-Encoding: gzip, deflate

    Response Header

    HTTP/1.1 200 OK
    Cache-Control: private
    Content-Type: text/xml; charset=utf-8
    Vary: Accept-Encoding
    Server: Microsoft-IIS/7.5
    X-AspNet-Version: 4.0.30319
    X-Powered-By: ASP.NET
    Date: Fri, 07 Jun 2013 06:38:42 GMT
    Content-Length: 145639

    • Edited by Sagart04 Friday, June 7, 2013 8:19 AM
    Friday, June 7, 2013 8:16 AM
  • I usually use wireshark and not fiddler so I not familar with the the wasy fiddler displays data.  Wireshark will automatically show the deflated gzip data and not the packed gzip format.  I don't know if fiddler does the same.  Look at the actual data packets and see if you get the correct response.  It appears everything is working properly.  I think the only reason you think the data isn't compressinmg is because fiddler is automatically decompressing the data.  The response header indicates the message was using gzip.

    There are three things that I notice in the header you posted that is important

    1) The data is HTTP/1.1 (not 1.0).  This probably means the data is being sent in chunk mode.

    2) The response status is "200 OK" which means you got all the data.

    3) The content length is 145639


    jdweng

    Friday, June 7, 2013 8:52 AM
  •       I have added client side code which you have suggested, by referring following link

    http://www.codeproject.com/Articles/12698/HTTP-compression-in-NET-Framework-2-0

       also i have run following command in command promptby using following post

    http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/d9eb22c4-0012-4496-8e58-ff32d68442ba

    cscript.exe adsutil.vbs set W3Svc/Filters/Compression/GZIP/HcScriptFileExtensions "asp" "dll" "exe" "svc"

    cscript.exe adsutil.vbs set W3Svc/Filters/Compression/GZIP/HcDynamicCompressionLevel 9

          I try following code in wcf to add compression in httpResponse as sujjested in following post

    http://social.msdn.microsoft.com/forums/en-US/wcf/thread/ddfe06b1-f07c-4da2-a1f1-d06126e4f96e/

      
       string acceptEncoding = HttpContext.Current.Request.Headers["Accept-Encoding"];
    if (!string.IsNullOrEmpty(acceptEncoding) && acceptEncoding.IndexOf("gzip", StringComparison.OrdinalIgnoreCase) > -1)
    {
      HttpContext.Current.Response.Filter = new GZipStream(
        HttpContext.Current.Response.Filter,
        CompressionMode.Compress);
      HttpContext.Current.Response.AppendHeader("Content-Encoding", "gzip");
    }

        Then also wcf response is not compressing. I got following information in fidder

    Request Header

    POST /LiteServicetInternet/Demo/DealMemoService.svc HTTP/1.1
    Content-Type: text/xml; charset=utf-8
    VsDebuggerCausalityData: uIDPo29XgKxCkCRClgS1QfX7j6gAAAAAtpYJMTHF4UeGDojLOIHqF/v2tRgMHXdFtIe/6f5yjskACQAA
    SOAPAction: "http://tempuri.org/IDealMemo/SearchCompetitorForLookUp"
    Host: ntvmsynrpt0389
    Content-Length: 819
    Expect: 100-continue
    Accept-Encoding: gzip, deflate


      This work for me that WCF service now sending compressed data.
    Monday, June 17, 2013 11:28 AM