Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8651989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:12:10+00:00 2026-06-12T14:12:10+00:00

I’m having trouble enabling GZIP compression on my C#.NET WCF web service and was

  • 0

I’m having trouble enabling GZIP compression on my C#.NET WCF web service and was hoping someone might know what I’m missing in my App.conf configuration file, or what extra is needed when making the call to start the web service in the code.

I’ve followed the link Applying GZIP Compression to WCF Services that points to the download of Microsoft example of adding GZIP but the example does not correlate with how I’m setting up my web service.

So my App.conf looks like

<?xml version="1.0"?>
<configuration>
  <system.serviceModel>
    <services>
      <service name="MyService.Service1">
        <endpoint address="http://localhost:8080/webservice" binding="webHttpBinding" contract="MyServiceContract.IService"/>
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior>
          <webHttp />
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <extensions>
      <bindingElementExtensions>
        <add name="gzipMessageEncoding" type="MyServiceHost.GZipMessageEncodingElement, MyServiceHost, Version=4.0.0.0, Culture=neutral, PublicKeyToken=null" />
      </bindingElementExtensions>
    </extensions>
    <protocolMapping>
      <add scheme="http" binding="customBinding" />
    </protocolMapping>
    <bindings>
      <customBinding>
        <binding>
          <gzipMessageEncoding innerMessageEncoding="textMessageEncoding"/>
          <httpTransport hostNameComparisonMode="StrongWildcard" manualAddressing="False" maxReceivedMessageSize="65536" authenticationScheme="Anonymous" bypassProxyOnLocal="False" realm="" useDefaultWebProxy="True"/>
        </binding>
      </customBinding>
    </bindings>
  </system.serviceModel>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
</configuration>

I simply copied the config and GZIP classes from the MS example into my project and added my relevent web service configs.
The code I’m using to start the windows service is:

WebServiceHost webserviceHost = new WebServiceHost(typeof(MyService.Service1));
webserviceHost.Open();

The webservice runs fine but Fiddler does not detect any response coming back with GZIP compressesion when making a call from a web browser. I also tried programmatically to setup and run the webservice with GZIP but failed miserably. Being green I’m not sure what else I need to configure, any advice is greatful

I’ve delved deeper into this and found out that since I’m running the webservice as a WebServiceHost object that it must be overriding the custom GZIP binding in the app.conf file with the WebHTTPBinding object that WebServiceHost defaults to, which means anything coming out of the web service will not be encoded.
To get around this I figured that I would programmatically write the custom GZIP binding into the code

var serviceType = typeof(Service1);
var serviceUri = new Uri("http://localhost:8080/webservice");
var webserviceHost = new WebServiceHost(serviceType, serviceUri);
CustomBinding binding = new CustomBinding(new GZipMessageEncodingBindingElement(), new HttpTransportBindingElement());
var serviceEndPoint = webserviceHost.AddServiceEndpoint(typeof(IService), binding, "endpoint");
webserviceHost.Description.Endpoints[0].Behaviors.Add(new WebHttpBehavior { HelpEnabled = true });
webserviceHost.Open();

The problem is that it won’t allow a custom binding with the WebHttpBehavior. But if I remove the behavior then my REST webservice turns ugly and expects Stream objects as inputs in my contracts. I’m not sure how to configure behaviours so any help is greatful.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T14:12:11+00:00Added an answer on June 12, 2026 at 2:12 pm

    So Here’s the programmatic solution I’ve come up with after spending days on this. Note that I do not know how to configure the solution in the app.config file but only through code.
    Firstly follow this Link to obtain and fix the GZIP classes in Microsoft’s coding samples. Then use the below example code as a basis for configuring your own web service.

    //Some class class to start up the REST web service
    public class someClass(){
        public static void runRESTWebservice(){
            webserviceHost = new WebServiceHost(typeof(Service1), new Uri("http://localhost:8080));
            webserviceHost.AddServiceEndpoint(typeof(IService), getBinding(), "webservice").Behaviors.Add(new WebHttpBehavior());
            webserviceHost.Description.Behaviors.Add(new ServiceMetadataBehavior { HttpGetEnabled = true });
        }
    
        //produces a custom web service binding mapped to the obtained gzip classes
        private static Binding getBinding(){
            CustomBinding customBinding = new CustomBinding(new WebHttpBinding());
            for (int i = 0; i < customBinding.Elements.Count; i++)
            {
                if (customBinding.Elements[i] is WebMessageEncodingBindingElement)
                {
                    WebMessageEncodingBindingElement webBE = (WebMessageEncodingBindingElement)customBinding.Elements[i];
                    webBE.ContentTypeMapper = new MyMapper();
                    customBinding.Elements[i] = new GZipMessageEncodingBindingElement(webBE);
                }
                else if (customBinding.Elements[i] is TransportBindingElement)
                {
                    ((TransportBindingElement)customBinding.Elements[i]).MaxReceivedMessageSize = int.MaxValue;
                }
            }
            return customBinding;
        }
    }
    
    //mapper class to match json responses
    public class MyMapper : WebContentTypeMapper{
        public override WebContentFormat GetMessageFormatForContentType(string contentType){
            return WebContentFormat.Json;
        }
    }
    
    //Define a service contract interface plus methods that returns JSON responses
    [ServiceContract]
    public interface IService{
        [WebGet(UriTemplate = "somedata", ResponseFormat = WebMessageFormat.Json)]
        string getSomeData();
    }
    
    //In your class that implements the contract explicitly set the encoding of the response in the methods you implement
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public class Service1 : IService
    {
        public string getSomeData()
        {
            WebOperationContext.Current.OutgoingResponse.Headers[HttpResponseHeader.ContentEncoding] = "gzip";
            return "some data";
        }
    }
    

    I worked most this out by following this link.

    Note: It somewhat baffles me how Microsoft haven’t built GZIP natively into WCF being such an essential part of any REST webservice returning large sets of data.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I know there's a lot of other questions out there that deal with this
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.