How to change Location(Url) of a Web Service and Update Web Reference programmatically?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-01 13:40:56

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))}
  };
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!