views:

145

answers:

2

I have a web service client application that connects to a 3rd party's web service. The 3rd party has updated their web service so now my client app fails with SOAP exceptions. Unfortunately, I can't simply update my app to work with the new web service since some of my users will still be using the old web service. So basically I'd like to be able to work with either version of the web service using a single version of my client app.

Since Visual Studio generates a bunch of code that I compile against when a web reference is added, I now have two files: one generated for each version of the web service. Both of the APIs are actually identical, it is just the underlying SOAP messages that seem to have changed. I've put each of these generated files into their own namespace to avoid collisions. So now I'd like to be able to choose which namespace to use at runtime (based on a config parameter).

For example:

if (useOldVersion)
    using OldVersionNamespace;
else
    using NewVersionNamespace;

Obviously, this would never work since there would be no way to compile the code that references elements in this namespace. Is there an easy way to deal with this issue? Maybe I could use reflection to do what I need at runtime?

The only thing I can really think of would be to literally copy/paste my current code into two different files, the only difference being which namespace is used at the top. This seems like a really bad idea though.

+3  A: 

Make both sets of generated classes implement a common interface. This is pretty easy with partial types - you don't need to change the generated class at all, just create an appropriate interface and then a file with the same types in as the generated ones, saying that they implement the interface.

You then code to the interface, and pick which implementation to pick in one place based on a config file.

The additional benefit to this is you can then write unit tests against your code, mocking out the web service.

Jon Skeet
This is probably the most correct solution, but there are a bunch of classes (100+) each with a bunch of properties/methods/etc. I guess I am hoping for a quicker fix...
Jason
A: 

couldn't you use a compile time if?

#If (BuildType)
using X;
#else
using Y;
#endif

http://msdn.microsoft.com/en-us/library/4y6tbswk.aspx[link text][1]

this is quick and dirty. hope that helps!

N8
Whoops, just saw the new comment. you've run your self into a conundrum!anyway you can talk your web service provider to keep themselves backwards compatible? *crossses fingers*
N8