简体   繁体   中英

How to configure self hosted WCF service to compress messages

I've been looking online but I can't find a defined procedure or an example on how to configure a basicHTTP self-hosted WCF service to compress messages that servers sends to clients.

I would like to use GZIP or similar technology.

this is my application app.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>      
  <system.serviceModel>
    <services>
      <service name="Application.ApplicationServicesProxy" behaviorConfiguration="ApplicationSvcsBehave">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:9001/ApplicationSvcs"/>
          </baseAddresses>
        </host>
        <endpoint address="http://localhost:9001/ApplicationSvcs" binding="basicHttpBinding" contract="Application.IApplicationServices"/>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ApplicationSvcsBehave">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="True"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8"/>
  </startup>
</configuration>

This is how I create the service:

var host = new ServiceHost(typeof(ApplicationServicesProxy));
host.Open();

Services are defined in IApplicationServices that is inherited by ApplicationServicesProxy

[ServiceContract]
interface IApplicationServices
{
    [OperationContract]
    string[] MethodOne();
}

public class AppicationServicesProxy : IApplicationServices
{
    public string[] MethodOne()
    {
         // return a string value;
    }
}

And this is how I connect the client to the service

var ApplicationSvcs = new ApplicationSvcs.ApplicationServicesClient("BasicHttpBinding_IApplicationServices");

I am currently using .NET 4.8

I solved doing like this, message size went down fromm 300 Kb/s to 11 Kb/s so it really works and CPU is not very interested in that:

<system.serviceModel>
    <services>
      <service name="Application.ApplicationServicesProxy" behaviorConfiguration="ApplicationSvcsBehave">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:9001/ApplicationSvcs"/>
          </baseAddresses>
        </host>
        <endpoint address="" binding="customBinding" bindingConfiguration="BinaryCompressionBinding" contract="Application.IApplicationServices"/>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ApplicationSvcsBehave">
          <serviceMetadata httpGetEnabled="false" httpsGetEnabled="false" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <customBinding>
        <binding name="BinaryCompressionBinding">
          <binaryMessageEncoding compressionFormat ="GZip"/>
          <httpTransport decompressionEnabled="true"/>
        </binding>
      </customBinding>
    </bindings>
  </system.serviceModel>

and on client side

 <system.serviceModel>
    <bindings>
      <customBinding>
        <binding name="CustomBinding_IApplicationServices" >
          <binaryMessageEncoding compressionFormat="GZip"/>
          <httpTransport maxReceivedMessageSize="20000000"/>
        </binding>           
      </customBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost:9001/ApplicationSvcs" binding="customBinding"
        bindingConfiguration="CustomBinding_IApplicationServices" contract="ApplicationSvcs.IApplicationServices"
        name="CustomBinding_IApplicationServices" />          
    </client>
  </system.serviceModel>

SOAP messages can be very heavy but in my application I could not use REST so I hope that can be of any help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM