如何以编程方式更改Web服务的位置(网址)和更新Web参考? [英] How to change Location(Url) of a Web Service and Update Web Reference programmatically?

查看:107
本文介绍了如何以编程方式更改Web服务的位置(网址)和更新Web参考?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有网络服务:

http://127.0.0.1/something/someWS.asmx

我将其添加为我的应用程序的Web参考,但不会始终是Localhost ...它可能会更改为

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.

如何以编程方式更改Web参考的URL?简单吗?

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,一旦更改了它,如何以编程方式对其进行更新? (等同于右键单击并选择更新Web参考")

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

旁注:我要最终完成的工作是基于服务器上可用的asmx Web服务(service.Url)的可用方法的下拉列表

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)

推荐答案

正如约翰·桑德斯(John Saunders)所评论的那样,从技术上讲,您尝试与服务的两个版本进行通信的方式是不可能的.您正在尝试将编译/设计时操作(更新Web参考")与运行时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.

一种简便的方法是将问题视为与提供相似数据的2个完全不同的数据源进行通信.这是一种经过充分研究的方法,包含大量样本-数据存储库是搜索词之一.

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.

实施:

  • 每个服务版本的一个Web参考
  • 一个公开需要的数据的接口(可以从Web服务获取的数据)
  • 每个网络参考的界面的一种实现
  • 具有接口实现的集合(即用于将友好名称映射到接口实现的字典),可以选择任何数据源.

代码:

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))}
  };

这篇关于如何以编程方式更改Web服务的位置(网址)和更新Web参考?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆