How I use XCeed streaming compression component to return web service data

I have mentioned my use of the XCeed streaming compression for returning data from web services a number of times in this blog. Here is a post when I first discovered that it reduced a 2.9 minute download of  4 MB to 12 seconds! I have since mentioned it a few times but never showed exactly how it is coded. Last night Rod Paddock pinged me to find out if I thought that component would work for him and it turned out he had the exact same scenario as I have been using it for. Therefore I showed him my code and thought I would put it here as well.

The key to all of this is that the component compresses bytes, so whatever you are returning, you want to convert it to bytes first. I’m sure this may make some web service purists cringe, but is it any different than returning a binary attachment with MTOM – which is a W3C standard? (I’m open to further education on this, my purist friends!)

Anyway, back to stream compression.

Let’s say you have a .NET client to .NET service scenario and are writing both ends. (That is a setup to avoid any rotten tomatoes for using a dataset in this example 🙂 ).

On the web service end, I have a method that accepts the dataset, converts it to a byte array, compresses that into another byte array using XCeed QuickCompression class and then returns this compressed byte array.

Using ms As New System.IO.MemoryStream
 ds.WriteXml(ms)
 Dim bytearray(ms.Length) As Byte
 
bytearray = ms.GetBuffer
 
Dim CompressedBytes() As Byte
 
CompressedBytes = QuickCompression.Compress(bytearray, CompressionMethod.Deflated, CompressionLevel.Normal)
 
Return CompressedBytes
End Using 

On the client end, having called this web service operation, I decompress the received bytes into a new byte array, then read that byte array into a new DataSet. Et Voila!

Dim ds As New DataSet
Dim compressedBytes() As Byte = WSProxy.GetDataSetasCompressedBytes
Dim byteArray() As Byte
= QuickCompression.Decompress(compressedBytes)
Using ms As New
MemoryStream
  ms.Write(byteArray, 0, byteArray.Length)
  ms.Position = 0
 
ds.ReadXml(ms)
End Using

I remember when I was first looking for a means of doing this and reading about this component, it wasn’t obvious how to do this in a web service, so I had a pointer from someone in tech support as to how to accomplish this.

I have used this combined with WSE 2.0 and now with WSE 3.0 to protect this data in addition to compressing it. If you have really humongous files, you can combine compression with MTOM in WSE 3.0 as well. I’ll have to check this out with WCF at some point.

Don’t Forget: www.acehaid.org

WCF Article Series by the pros coming up in CoDe Magazine

I’m looking at Rod Paddock’s editorial in the current CoDe Mag issue. This is the future tehcnologies issue wiht articles on WPF, LinQ and Ajax. In the editorial he says that there is going to be a 6 article series on WCF starting with the next issue. I’m really looking forward to this. Especially considering who is going to be writing some of these: Michele Leroux Bustamante, Juval Lowy, Christian Weyer.

Don’t Forget: www.acehaid.org

Rich Turner is the Product Manager for InfoCard – yes,yup, da, oui, ja ja ja!

Sorry, couldn’t help it but I can’t think of Rich without thinkng of the phrase “No! Nope! Niet! Non! Negative! Nuh-huh! [shake head vigorously]!!!” in response to the question “is .net remoting dead?”

He’s off to be p.m. for InfoCards, whic makes perfect sense. I think outside of Microsoft, Michele is the one person who knows anything about InfoCards so far – but that too, is a natural given her work in interop.

Don’t Forget: www.acehaid.org

New INETA Speakers!!!!!

Wow, what a surprise but such a great one. INETA just added 5 new speakers

Scott Cate
Richard Hundhausen
Wally McClure
Sahil Malik
Robert Green (hey Robert, where’s your blog?)

Although out of this bunch I have only ever personally attended sessions by Richard, the other 4 guys are not only some of the top experts in their fields, have done a ton of presenting and written a host of books, but every one of them (including Richard) are really nice guys and will be awesome to have at your user groups.

Congrats guys.

Don’t Forget: www.acehaid.org

To chris, who wants to know how often I hit the “i” key when I blogged over the past 2 years

Sent: Friday, February 17, 2006 4:45 PM
Subject: why do you auto highlight your pages? (via Julia Lerman Blog)

It took my like 300,000K of my memory, 50% of my CPU and 10 minutes to load one of your pages.

Why do you torture your visitors by doing highlighting at all, and even worse, by doing it on their side?

I didn’t even read the content because I was so bothered by the fact that it was trying to highlight everything, including the letter ‘i’ whereever it occured.
 
****************************************************
 
Dear Chris,
 
Is this for real or does everyone with a dasblog get this email?
 
The only way you would possibly get this behavior is if you did a search (using the search tool built into dasblog) on the letter “i”.
 
Here is the url when you use that search tool to search for “julie” in my blog:
 
 
So clicking that link you will first get a definite lag time while everyone of my posts is searhced for the word “julie” and then when it renders (with EVERY post on my entire weblog that has this word in it), it will highlight in yellow the word “julie” on the resulting page.
 
So imagine doing the search in the letter “i”. It will take quite some time to search through all of my posts and then return hundreds and hundreds of posts which will take a long time to render and THEN it will go through that entire rendered page and highlight the “i” s. And if that took ONLY 10 minutes, you must have one fast machine!
 
On the other hand, if you are searching for every time I used the letter “i” in a post over the last 2+ years, you have much bigger things to worry about.
 
Julie