1

I have web service:

http://127.0.0.1/something/someWS.asmx

I am adding this as a Web Reference to my app but wont always be Localhost... it might change to http://www.something.com/something/someWS.asmx.

How do I change the URL programmatically of my Web Reference? is it as simple as:

using (var service = new MyApi.MyApi())
{
    //txtUrl is the site
    service.Url = "http://" + txtUrl + "something/someWS.asmx";
}

ALSO, once I change it, how do I update it programmatically? (equivalent to right-clicking and selecting "Update Web Reference")

side-note: What I am trying to ultimately accomplish is dropdowns of the available methods based on the asmx WebService available on the server (service.Url)

user1867353
  • 487
  • 1
  • 6
  • 12
  • possible duplicate of [Is it possible to change the properties of a WebReference in run-time?](http://stackoverflow.com/questions/211510/is-it-possible-to-change-the-properties-of-a-webreference-in-run-time) – John Saunders Aug 19 '13 at 19:40
  • They generate a constructor that takes the url as a parameter – user1985513 Aug 19 '13 at 19:43
  • will have different versions of the WebService, so its a different than the "possible" duplicate, thats why I want to know how I can Update the Web Reference programmatically after i have changed the URL :) – user1867353 Aug 19 '13 at 19:44
  • You should have said this in your question. The answer is: you can't, and it doesn't even make sense. If you have different versions of the service, then you have different services. What exactly are you trying to accomplish? – John Saunders Aug 19 '13 at 19:50
  • I'm trying to accomplish filling in ComboBoxes with the available methods in the WebService based on the URL. I figured if I updated the Web Reference, i could go from there and get all the methods. I want to update programmatically if possible (equivalent of right-clicking ans "update Web Reference") because as I add more methods to my Web Service, i would like to see them in my Test application Drop Downs (ComboBoxes) – user1867353 Aug 19 '13 at 19:57
  • 1
    ASMX is a legacy technology, and should not be used for new development. WCF or ASP.NET Web API should be used for all new development of web service clients and servers. One hint: Microsoft has retired the [ASMX Forum](http://social.msdn.microsoft.com/Forums/en-US/asmxandxml/threads) on MSDN. – John Saunders May 20 '14 at 21:02
  • possible duplicate of [ASMX: setting the website at runtime](http://stackoverflow.com/questions/2997885/asmx-setting-the-website-at-runtime) – KyleMit Aug 19 '14 at 15:15

1 Answers1

1

As John Saunders commented the way you trying to take to talk to 2 versions of a service is not technically possible. You are trying to mix compile/design time action ("update Web reference") with runtime one.

Easy approach would be to look at the problem as talking to 2 completely different data sources providing similar data. This is well researched approach with plenty of samples - data repository is one of the search terms.

Implementation:

  • one web reference per version of the service
  • an interface that exposes data you need (the one you can obtain from web service)
  • one implementation of the interface per web reference
  • have collection of interface implementations (i.e. dictionary to map friendly name to interface implementation) that allows to pick any data source.

Code:

interface IMyData 
{
      string GetLastName();
}

class MyDataFromOldWebService
{
    MyApi.MyApiV1 service;
    MyDataFromOldWebService(MyApi.MyApiV1 service)
    {
      this.service = service;
    }
    public string GetLastName()...
}

Dictionary<string, IMyData> services = new Dictionary<string, IMyData>()
  {
      { "Old Service", new MyDataFromOldWebService(new MyApi.MyApiV1(url))}
  };
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179