c#

WCFでGZip圧縮(WCF Client と Apache CXF + pjl-comp-filter) - その2


その2です。

HTTPS での GZip圧縮が出来たので、掲載しておきます。
WCFクライアントだけね。サーバー側は前 回と変わりません(pjl-comp-filterでGzipフィルタかけてるだけ )ので。
注意点だけ赤色にしておきます。


以下参照。

[ Program.cs ]
-----------------
using System;
using System.Collections.Generic;
using System.Text;
using ims;
using System.ServiceModel;
using System.ServiceModel.Channels;
using Microsoft.ServiceModel.Samples;
using System.Net;

namespace WCF_TEST2 {
    class Program {
        static void Main(string[] args) {
            try {
                show();
            } catch (Exception e) {
                Console.WriteLine("msg: " + e.Message);
                Console.WriteLine("data: " + e.Data);
                Console.WriteLine("src: " + e.Source);
                Console.WriteLine("trace: " + e.StackTrace);
            }
        }

        private static void show() {
            string Address = Properties.Settings.Default.Address;
            string Binding = Properties.Settings.Default.Binding;
            ServiceClient service = null;

            Console.WriteLine("Call HTTPS messge: ");
            //証明書の確認用EvnetHandlerをコール
            ConfirmCertificatePolicy.ConfirmCertificatePolicy_CallEventHandler();

            ICollection<BindingElement> bindingElements = new List<BindingElement>();
            HttpsTransportBindingElement httpsBindingElement = new HttpsTransportBindingElement();
            string proxyEnabled = Properties.Settings.Default.ProxyEnabled;
            if (proxyEnabled == "TRUE") {
                string proxyServer = Properties.Settings.Default.ProxyServer;
                httpsBindingElement.BypassProxyOnLocal = true;
                httpsBindingElement.UseDefaultWebProxy = false;
                httpsBindingElement.ProxyAddress = new Uri(proxyServer);
            }

            GZipMessageEncodingBindingElement compBindingElement = new GZipMessageEncodingBindingElement();
            bindingElements.Add(compBindingElement);
            bindingElements.Add(httpsBindingElement);

            CustomBinding myBinding = new CustomBinding(bindingElements);
            myBinding.Name = Binding;
            myBinding.Namespace = "http://ws.chocbanana.com/";


            // Webサービスインスタンス生成 //
            service = new ims.ServiceClient(myBinding, new EndpointAddress(Address));

            HttpRequestMessageProperty httpRequestProperty = new HttpRequestMessageProperty();

            //application/x-gzip
            httpRequestProperty.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip");
            httpRequestProperty.Headers.Add(HttpRequestHeader.ContentEncoding, "gzip");

            using (OperationContextScope scope = new OperationContextScope(service.InnerChannel)) {
                OperationContext.Current.OutgoingMessageProperties
                [HttpRequestMessageProperty.Name] = httpRequestProperty;
                Console.WriteLine(service.hello());
            }
        }
    }
}
[ EOF ]
-----------------

[ app.config ]
-----------------
<?xml version="1.0"?>
<configuration>
  <configSections>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <section name="WCF_TEST2.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
    </sectionGroup>
  </configSections>

  <system.serviceModel>
    <behaviors>
      <endpointBehaviors>
        <behavior name="ClientCredentialsBehavior">
          <clientCredentials>
            <serviceCertificate>
              <authentication certificateValidationMode="None" revocationMode="NoCheck"/>
            </serviceCertificate>
          </clientCredentials>
        </behavior>
      </endpointBehaviors>
    </behaviors>

    <extensions>
      <bindingElementExtensions>
        <add name="gzipMessageEncoding" type="Microsoft.ServiceModel.Samples.GZipMessageEncodingElement, GZipEncoder, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
      </bindingElementExtensions>
    </extensions>

    <bindings>
      <customBinding>
        <binding name="GzipSecureBinding_Service">
          <gzipMessageEncoding innerMessageEncoding="textMessageEncoding" />
          <sslStreamSecurity requireClientCertificate="false"/>
          <httpsTransport manualAddressing="false" authenticationScheme="None"
            bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
            proxyAuthenticationScheme="Basic" realm="" useDefaultWebProxy="false" />

        </binding>
      </customBinding>
    </bindings>

    <client>
      <endpoint address="https://127.0.0.1:8443/services/Service" binding="customBinding"
        bindingConfiguration="GzipSecureBinding_Service" contract="ims.IService" behaviorConfiguration="ClientCredentialsBehavior"
         name="CustomBinding_Service" />
      <metadata>
        <policyImporters>
          <extension type="Microsoft.ServiceModel.Samples.GZipMessageEncodingBindingElementImporter, GZipEncoder, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
        </policyImporters>
      </metadata>
    </client>
  </system.serviceModel>

  <applicationSettings>
    <WCF_TEST2.Properties.Settings>
      <setting name="ProxyServer" serializeAs="String">
        <value>http://proxyserver:8080/</value>
      </setting>
      <setting name="ProxyEnabled" serializeAs="String">
        <value>FALSE</value>
      </setting>
      <setting name="Binding" serializeAs="String">
        <value>CustomBinding_Service</value>
      </setting>
      <setting name="Address" serializeAs="String">
        <value>https://chocbanana.com/services/Service</value>
      </setting>
    </WCF_TEST2.Properties.Settings>
  </applicationSettings>
  <startup>
    <supportedRuntime version="v2.0.50727"/>
  </startup>
</configuration>
[ EOF ]
-----------------

このページの先頭へ