分享
 
 
 

Changing Target Web Service At Runtime

王朝other·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

简介

随着web服务的不断发展,其客户端引用也不断的发展壮大,适时、动态的依据.asmx文件所在的URL来添加web引用成为趋势。

当我们加载了一个web服务以后,这个web服务有可能会更换URL地址。这样会让我们引用的服务失效,这是我们不愿看到的事情。那么如何才能在更改web services的URL地址以后,web应用还能继续生效呢?

下面的列子将会告诉你。

首先我们创建一个只有一个mothod的web服务。

Create a new C# web service project in VS.NET.

Open the default .asmx file and add following code to it.

using System;

using System.Web.Services;

namespace HelloWorldWS

{

public class CHelloWorld :

System.Web.Services.WebService

{

[WebMethod]

public string GetHelloWorld()

{

return "Hello World From CHelloWorld";

}

}

}

As shown above this web service class (CHelloWorld) contains a single method called GetHelloWorld() that returns a string.

Add another .asmx file to the project.

Open the file and modify it as shown below. using System;

using System.Web.Services;

namespace HelloWorldWS

{

public class CHelloWorldBackup :

System.Web.Services.WebService

{

[WebMethod]

public string GetHelloWorld()

{

return "Hello World From CHelloWorldBackup";

}

}

}

This class is similar to previous one but its name is CHelloWorldBackup. Also, it returns different string from GetHelloWorld() method so that you can identify the method call

Now, that we have both the web services ready compile the project.

下面我们创建客户端程序来加载这个web服务

Create a new ASP.NET web application in VS.NET.

The application will have a default web form. Before writing any code we need to add a web reference to our web service. Right click on the references node and select Add web reference. Follow the same procedure as you would have while developing normal web services. Adding a web reference will generate code for proxy web service object.

Place a button on the web form and add following code in the Click event of the button: private void Button1_Click

(object sender, System.EventArgs e)

{

localhost.CHelloWorld proxy=new localhost.CHelloWorld;

Response.Write(proxy.GetHelloWorld());

}

Above code shows how you will normally call a web service. The web reference contains information about the location of the web service.

If you move the .asmx file after you depoly this client, it is bound to get an error. To avoid such situation, modify above code as shown below: private void Button1_Click

(object sender, System.EventArgs e)

{

localhost.CHelloWorld proxy=new localhost.CHelloWorld;

proxy.Url="http://localhost/webserviceurlandtimeout

/HelloWorld.asmx";

Response.Write(proxy.GetHelloWorld());

}

In above code we have explicitly set Url property of the proxy class to the required .asmx file. You can easily store this URL in <appSettings> section of web.config file and retrieve it at run time. Now, even if you move your web service, all you need to do is change its URL in the web.config.

Following code shows this: private void Button1_Click(object sender, System.EventArgs e)

{

localhost.CHelloWorld proxy=new localhost.CHelloWorld;

proxy.Url=GetURL();

Response.Write(proxy.GetHelloWorld());

}

public string GetURL()

{

return ConfigurationSettings.AppSettings["webserviceurl"];

}

The web.config looks like this: <appSettings>

<add

key="webserviceurl"

value="http://localhost/webserviceurlandtimeout

/HelloWorldBackup.asmx" />

</appSettings>

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有