Archive for the ‘C#’ Category

C# - web service tip - extending web service classes

Saturday, May 10th, 2008

When generating a web service class by the WSDL generator, it overrides what was there regardless of modification.  One of the thing that bugs me is that some of the classes it generates, I would put a property that would return itself (for data binding).  So, everytime I would update the web service, that property would get lost.  It wouldn’t be a problem, but it’s not just one class, its a couple classes, and this project does not just have one web service, it has many.

So, to fix this problem, I was thinking of everything… from using reflection to create the property for every web service, to creating a macro in the IDE so everytime I do an update, it would automatically create it for me.

Then yesterday, I was looking at a class for another issue.  Then I asked myself, why is there a ‘partial’ keyword for the class.  I know what partial does, but for a web service class (????)… why?  Then I pulled up MSDN and read more on partial.  It says that it can help with developers modifying the same class without modifying the same file… (hm…. the gears are going now…).  Then I think… could I have a partial file thats not in the same directory as another file… I did a test partial file in another file.  Changed the namespace to match the original namespace.  Built it… no errors…. ran it… no problems.  OH….. i have now seen the light.

So, did an update to my web service, created a partial class in another directory, created the property in that file, then buildt and ran my code.  Everything was working.  This was great, that means I can extend web service classes without having the fear of forgetting what properties I need.

Summary… let the web service class generator do it’s thing.  Create another class (doesn’t need to be in the same directory) with the same name, change the namespace to match the original generated class.  Now, you’re ready to go.

C# - web service tip - compression

Saturday, May 10th, 2008

So, I’ve learned alot about C# because one of my projects I chose to go with C#. Well, here’s a little background. We have a linux server that will host the web service (using NuSoap). Then I have a windows client which is developed on C#. One thing that I know is that the data going back and forth will get large (around couple KBs). So, I’ve been trying to figure out how to use compression.

NuSoap has built in compression by checking if the HTTP headers if it supports it. So, I need to modify the clients header to tell the server it can receive compressed data. Basically for each service that .NET creates by WSDL, you need to override the GetResponseStream and GetRequestStream function.

GetRequestStream is where you would modify the headers to accept gzip.

GetResponseStream is where the decompression takes place when the server sends the data over.

I won’t write any code here, but you can search on google for some sample code. Real easy too.

After doing this, I did some analysis. Original data -> ~7KB, compressed data -> ~800B (about 90% reduced). Original data -> ~5KB, compressed data -> ~800B (about 80% reduced).

Sure… I’m not sending data that is huge, but this is for a simple case, and for future features that are intense with data, this will help.