views:

29

answers:

1

Can web services be used in a factory pattern given that the code is auto-generated and I do not want to alter it (to add a base class for example)?

A reason to do this would be if you had 2 web services that were identical but one was for test data and one was for live data and you wanted to switch between the services based on the environment the code was runnig in.

[Edit]
I am using C# 3.

A: 

If you're using C# and SOAP, you can change the destination at runtime:

var webSvc = new WebServerObjectName();
webSvc.Url = "http://examples/com/foo.asmx"; 

//or pull from .config, etc.  
webSvc.Url = ConfigurationManager.AppSettings["WebServiceUri"].ToString();

//make the call to the web method
var custs = webSvc.GetCustomerList();

The flow would be:

  • at design-time, make the web reference. Establish the contract, and code for it (input & output params). You'll only need to make it once, as long as the contract stays the same.
  • at run-time, change the URL/URI/target of the web service. Obviously it would have to have the same contract/params/method signature, otherwise the call would fail at runtime.
  • make the call
p.campbell
Thank you for responding, I'm not clear on how this would work when using a web reference. Do I create 1 web reference and just change the URL at runtime?
DaveC
@DaveC: yes, you'd only have to make the web reference once. If we're talking about .NET, that really just makes the proxy classes to abstract the HTTP details. So yes, just once, and change the URL endpoint at runtime before the webmethod call.
p.campbell