0

I've searched a lot for this but I always get results for passing parameters to a web service. I want to pass a web service object as a parameter into a method if this is possible. My current code is below.

 public static string CheckDaysLeft(string serial) { amazonaws.WebService1 MyService = new amazonaws.WebService1(); return MyService.GetDaysLeft(serial).ToString(); } 

So I have two web services references in my project, localserver and the above amazonaws. Above I am calling this method which contacts the amazon web service and gets the days left for a particular serial number. This is working fine. I want to be able to pass as a parameter into the CheckDaysLeft method which of the two web services to use (localserver or amazonaws). The reason being that if one web service is down, the program will attempt to communicate with the other. I could create the CheckDaysLeft method twice and use the each web service reference in each method but that is not ideal.

How can I do this?

5
  • Do those web services share a common base class or interface? Commented Mar 10, 2016 at 9:01
  • Yes they are the exact same. Commented Mar 10, 2016 at 9:04
  • 1
    What Daniel means is: Do they implement a common interface like IMyService or a base class like BaseService? In the worst case, the common class would be object. You could then use the is operator to check which one you got and perform a cast. Another possible way would be to wrap the service calls into another "proxy" class that has references to both services and performs the calls on the one or the other, so you only have to pass instances of the "proxy". Commented Mar 10, 2016 at 9:05
  • 1
    @nerdalert I don't think that this is true. They might have the same methods, but do they actually derive from the same class or implement the same interface? Commented Mar 10, 2016 at 9:05
  • Ah my mistake. Honestly I'm not sure but probably not. They share the same methods. They both share a WebService1.asmx page with the same methods. Commented Mar 10, 2016 at 9:12

2 Answers 2

1

If they have no common base class or interface, I can think of two ways of achieving what you want:

Pass an object
Make your method take an object parameter and use the is operator to find out what you got, maybe like this:

public void CallMethod(object service) { if (service is Localservice) ((Localservice)service).CallMethod(); else ((Amazonservice)service).CallMethod(); } 

Create a proxy class
You might also encapsulate every call in a proxy class that has both service references and decides which one to use. You can then rely on the proxy class to do the right thing:

public class ProxyClass { private Localservice _localService; private Amazonservice _amazonService; public ProxyClass(Localservice instance1, Amazonservice instance2) { _localService = instance1; _amazonService = instance2; } public void CallMethod() { if (shouldUseLocalService) _localService.CallMethod(); else _amazonService.CallMethod(); } } 

You can then make your actual method like this:

public void CallMethod(ProxyClass proxy) { proxy.CallMethod(); } 
Sign up to request clarification or add additional context in comments.

2 Comments

Good answer. I have one suggestion though: First checking with is and then performing a safe cast via as is considered bad style. Either check with is and perform a direct cast ((Localservice)service).CallMethod() or - better - ditch is, use as and check for null: var localService = service as Localservice; if (localService != null) localService.CallMethod(); else ((Amazonservice)service).CallMethod();
Thank you for the thorough examples! When attempting the first method Pass an object, I get errors 'test.amazonaws' is a 'namespace' but is used like a 'type' for both web references.
0

If both of your servers use the same method (Object.GetDaysLeft(serial)), you could always create (if not already using a common one) an interface that you can then implemement in both classes, say IMyServers. That way, you can do something along the lines of:

public static string CheckDaysLeft(string serial, IMyServer Server) { return Server.GetDaysLeft(serial).ToString(); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.