WCF over HTTPS, Compression, and Binary Binding
Join the DZone community and get the full member experience.Join For Free
i have an application that is composed by wpf clients deployed over the internet and a central wcf service exposed over https for security reasons . everything works fine, but as the number of clients grows, performance starts to decrease a little. since i have a great number of clients that operates behind a single standard adsl 8 mbit (used for everything, mail, browsing, etc..), i measured and found that bandwidth is becoming a bottleneck, so i need to reduce the amount of traffic between clients and server .
it turns out that the easiest way to gain bandwidth is enabling iis dynamic compression , as you can read on this interesting article by scott hanselman that explains in great details how you can enable compression in iis. since my server has plenty of cpu available, compression is a really interesting way to reduce bandwidth.
this is not the only option -- you can also enable binary serialization over http because binary serialization usually is smaller than standard serialization you obtain with datacontractserializer . the only drawback is that such an endpoint is accessible only with wcf client; but if this is an issue it is really simple to expose the same service with multiple endpoints, each one with a different binding. i decided to add another endpoint with custombinding to use the binarymessageencoding (because i want to be interoperable with non .net clients), so i added this binding configuration
<custombinding> <binding name="binaryhttpbindingwithnoauth"> <binarymessageencoding> <readerquotas maxdepth="32" maxstringcontentlength="2100000000" maxarraylength="16384" maxbytesperread="2147483647" maxnametablecharcount="16384" /> </binarymessageencoding> <httpstransport /> </binding> <binding name="binaryhttpbinding"> <binarymessageencoding> <readerquotas maxdepth="32" maxstringcontentlength="2100000000" maxarraylength="16384" maxbytesperread="2147483647" maxnametablecharcount="16384" /> </binarymessageencoding> <security authenticationmode="usernameovertransport"> </security> <httpstransport /> </binding> </custombinding>
then i added another endpoint of this service that use this binding (actually i have more than a single wcf service, some of them without authentication, some other that needs to be authenticated, this is the reason why i need two configuration for binding).
then i modified my client program to be able to use standard or binary endpoint with a user settings and i started to measure bandwidth usage in various situation. i’ve started with a real scenario, startup of the application with production real data , because during startup the application issues seventeen requests to the service to initialize everything, it start requesting customer information, then try to login the user and finally loads all data the user needs to start working. i can improve performances reducing number of requests, but actually i’m interested only in download bandwidth.
here is the sum of bytes downloaded for all the requests, first column is soap uncompressed, second one soap compressed, third column binarymessageencoding uncompressed, and finally fourth column binarymessageencoding compressed.
figure 1: measurement over the amount of data downloaded at application startup
with soap binding and no compression the startup download ~262kb of data and since data is mostly textual, simply enabling http dynamic compression (second column) saved ~80% of the bandwidth , because total amount of data is now ~52kb. now it is interesting to compare these numbers with binarymessageencoding (third column) because the total size is ~155kb, a ~40% saving respect soap uncompressed data . this is interesting because if for some reason you have small cpu power to enable compression, with binary encoding you can still save a lot of bandwidth without resorting to compression. the fourth column represents binaryencoding with dynamic compression, since the content is mostly textual i can gain ~76% of the bandwidth (36kb instead of 155kb) enabling compression even for the binary encoding . it is a ~30% save respect to compressed soap and ~86% gain respect to soap uncompressed.
these numbers can vary a lot in your situation, because they are highly dependent on the format of the data (i need to send large portion of textual data so my data is highly compressible), but it takes some measurement to understand if you can gain a similar amount of bandwidth with your data. the important stuff is that everything is done simply by changing wcf configuration, and you do not need to change a single line of code.
Published at DZone with permission of Ricci Gian Maria, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.