I’m trying to create a C# class that can call the same methods on the same classes, but the classes can come from two different web service references. I am accessing SomeWebService and SomeWebServiceAlt, where they have the same classes but are on different servers. The goal is to be able to first try using the methods in SomeWebService, and if that one wigs out, to try SomeWebServiceAlt. I was hoping to have some kind of generic class for accessing either web service, but I’m having trouble with type constraints because there aren’t any common interfaces or base classes for the different classes I use from within either reference. That is, I use ConnectionClass from both the SomeWebService namespace and the SomeWebServiceAlt namespace, but the two ConnectionClass classes are not seen as related by C#, so I can’t just say where TClass : CommonBaseClassAcrossWebServices. How should I do this? I’m ending up repeating code and doing:
if (typeof(SomeWebService.ConnectionClass) == typeof(TConnection))
{ ... }
else if (typeof(SomeWebServiceAlt.ConnectionClass) == typeof(TConnection))
{ ... }
This seems really redundant and dirty. I’m using .NET 3.5, and I don’t have control over the two web services. That’s a difference between my question and similar questions I’ve seen on here: other people had control of the web services.
I feel like my solution is going to end up using reflection to somehow check and see if an expected method exists, and then call that method on whichever web service’s class I’m working with.
If they share the same service definition you can use the same reference, just change the host at runtime.