SilverlightアプリケーションとWCFサービス間をバイナリ形式で通信する

SilverlightアプリケーションとWCFサービス間を、Binary形式のデータで通信をするための設定について書きます。

アジェンダ

  • サーバのWeb.configの設定
  • クライアントのServiceReferences.ClientConfigの設定
  • Binary形式で通信を行っているか確認する

サーバのWeb.configの設定

WCFサービスのEndopointをBinaryBindingに設定し直します。なお、Silverlight用のBinaryBindingは、CustomBinding クラスにMessageEncodingBindingElement クラスを設定することにより実現します。

Web.confingのsystem.serviceModelタグを示します。

<system.serviceModel>
  <behaviors>
    <serviceBehaviors>
      <behavior name="Service1Behavior">
        <serviceMetadata httpGetEnabled="true" />
        <serviceDebug includeExceptionDetailInFaults="false" />
      </behavior>
    </serviceBehaviors>
  </behaviors>
  <bindings>
    <customBinding>
      <binding name="customBinding0">
        <binaryMessageEncoding />
        <httpTransport />
      </binding>
    </customBinding>
  </bindings>
  <services>
    <service 
        behaviorConfiguration="Service1Behavior"
        name="WCFHelloWorld.Web.HelloWorldService">
      <endpoint 
          address="" 
          binding="customBinding" 
          bindingConfiguration="customBinding0"
          contract="WCFHelloWorld.Web.IHelloWorldService" />
      <endpoint 
          address="mex" 
          binding="mexHttpBinding" 
          contract="IMetadataExchange" />
    </service>
  </services>
</system.serviceModel>

各タグの属性値の関連イメージを示します。

クライアントのServiceReferences.ClientConfigの設定

サーバの設定完了後、クライアントをサーバの設定に合わせます。
VisualStudioのソリューションエクスプローラから、右クリックでサービス参照の更新を行います。
これで、ServiceReferences.ClientConfigがサーバの設定に合わせて下記のようになっているはずです。

<configuration>
    <system.serviceModel>
        <bindings>
            <customBinding>
                <binding name="CustomBinding_IHelloWorldService">
                    <binaryMessageEncoding />
                    <httpTransport maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" />
                </binding>
            </customBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:52627/HelloWorldService.svc"
                binding="customBinding" bindingConfiguration="CustomBinding_IHelloWorldService"
                contract="HelloWorld.IHelloWorldService" name="CustomBinding_IHelloWorldService" />
        </client>
    </system.serviceModel>
</configuration>

Binary形式で通信を行っているか確認する

IISWCFサービスをデプロイし、FiddlerでBinary通信をおこなえている確認します。下記の記事を参照してください。

実際のFiddlerでのモニタ結果です。

Binaryに変換されていることがわかります。