SilverlightアプリケーションとWCFサービス間を、Binary形式のデータで通信をするための設定について書きます。
アジェンダ
- サーバのWeb.configの設定
- クライアントのServiceReferences.ClientConfigの設定
- Binary形式で通信を行っているか確認する
サーバのWeb.configの設定
WCFサービスのEndopointをBinaryBindingに設定し直します。なお、Silverlight用のBinaryBindingは、CustomBinding クラスにMessageEncodingBindingElement クラスを設定することにより実現します。
- CustomBinding クラス (System.ServiceModel.Channels)
- MessageEncodingBindingElement クラス (System.ServiceModel.Channels)
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形式で通信を行っているか確認する
IISにWCFサービスをデプロイし、FiddlerでBinary通信をおこなえている確認します。下記の記事を参照してください。
- VisualSudioからIISへWCFアプリケーションをデプロイする - y_maeyamaの日記
- 他ドメインのWebサービスにアクセスする方法 - y_maeyamaの日記
- WCFサービスのエンドポイントを変更する - y_maeyamaの日記
- Fiddler2を使ってサーバとSivlerlightアプリケーションアプリ間の通信内容を確認する。 - y_maeyamaの日記
実際のFiddlerでのモニタ結果です。
Binaryに変換されていることがわかります。