C# - web service tip - extending web service classes
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.