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