WCF and Silverlight - Binary Encoding
I have an odd problem. I am switching from HttpBasicEncoding to binary encoding. When I switch to use binary message encoding, I have noticed in Fiddler that my body of the response is 20x LARGER than the HttpBasicEncoding Equivalent. Here are my settings on the server:
When using Basic Encoding
<endpoint address="" binding="basicHttpBinding" bindingConfiguration="MyBinding"
contract="MyProduct.MyService" />
...
<basicHttpBinding>
<binding name="MyBinding">
<security mode="TransportCredentialOnly" />
</binding>
</basicHttpBinding>
When Using Binary Encoding
<customBinding>
<binding name="MyBinaryBinding">
<binaryMessageEncoding />
<httpTransport />
</binding>
</customBinding>
<endpoint address="" binding="customBinding" bindingConfiguration="MyBinaryBinding"
contract="MyProduct.MyService" />
In the Silverlight application, I need to programmatically build the binding. Here is how I am building those bindings:
When using Basic Encoding
BasicHttpBinding binding = new BasicHttpBinding();
binding.Security.Mode = BasicHttpSecurityMode.None;
// Wireup request
When using Binary Encoding
HttpTransportBindingElement transportElement = new HttpTransportBindingElement();
transportElement.MaxReceivedMessageSize = int.MaxValue;
transportElement.MaxBufferSize = int.MaxValue;
CustomBinding binding = new CustomBinding();
binding.Elements.Add(new BinaryMessageEncodingBindingElement());
binding.Elements.Add(transportElement);
// Wireup request
What am I doing wrong? Why is binary encoding response so much larger in size than the HTTP response?
Th开发者_运维知识库ank you!
BinaryFormatter and NetDataContractSerializer include a lot of type metadata and field info, and it adds up. For generating much smaller binary, I would recommend protobuf-net (disclosure: in the author), but note that I don't have a direct serializer swap-in for WCF on Silverlight so you would has to send byte[] as the args and handle manually, but it is small and fast. I want to improve that area.
Actually, with that setup regular http binding is then better, especially if you can enable MTOM. I hope to improve the Silverlight usage in time.
精彩评论