Changing Target Web Service At Runtime

原创 2003年04月08日 09:05:00

简介

随着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>    

Eclipse 中web项目报 target runtime apache tomcat v7.0 is not defined.

Eclipse 中web项目报 target runtime apache tomcat v7.0 is not defined. 在“错误”视图中直接右击删除出错的条目就可以了。 如果不行,试试...
  • yx13649017813
  • yx13649017813
  • 2014年01月15日 23:04
  • 6644

IWAB0489E Error when deploying Web service to Axis runtime

错误详情: IWAB0489E Error when deploying Web service to Axis runtime   axis-admin failed with  {http:/...
  • fujianianhua
  • fujianianhua
  • 2014年12月08日 14:40
  • 6199

解决IWAB0489E Error when deploying Web service to Axis runtime

WebEclipse应用服务器WebServiceSOAP  eclipse europa自带web工具。我们可以使他生成动态web程序。但是在默认情况下,生成的动态默认程序是不包含we...
  • b222007
  • b222007
  • 2013年07月12日 11:04
  • 3666

Type Target runtime MyEclipse Tomcat v7.0 is not defined.(Eclipse导入工程)

当Eclipse导入工程(尤其其他人的)后工程上显示红叉,解决方案: 需要解决一下类库路径问题:【项目】->【右键】->【build path】->【configure build path】-...
  • miaomiao0306
  • miaomiao0306
  • 2016年11月19日 10:22
  • 320

用eclipse 自带的工具生成 webservice时产生IWAB0489E Error when deploying Web service to Axis runtime

1、错误描述: 用eclipse把普通的web程序生成webservice程序时,会在启动服务器时出现如下错误:IWAB0489E Error when deploying Web service ...
  • VampiresLove
  • VampiresLove
  • 2011年10月17日 19:26
  • 4235

解决The Apache Axis2 Web service runtime in Tomcat v7.0 Server does not support the service project 问题

转载自http://blog.163.com/xueling1231989@126/blog/static/102640807201501383952762/ 按照教程配置Axis2,出现如...
  • ystyaoshengting
  • ystyaoshengting
  • 2015年04月15日 14:52
  • 3738

Project facet Dynamic Web Module version 3.0 is not supported

背景:web项目无法部署到JBoss服务器上,出现“Project facet Dynamic Web Module version 3.0 is not supported”错误。        ...
  • liusong0605
  • liusong0605
  • 2013年04月24日 21:51
  • 8391

使用 Rational Application Developer V7.0 开发 JAX-WS Web 服务

原文:http://www.ibm.com/developerworks/cn/rational/r-cn-radv7jax-ws/
  • lan861698789
  • lan861698789
  • 2014年05月26日 22:25
  • 885

Eclipse 项目更换电脑后提示Target runtime is not defined的解决办法

Eclipse 项目更换电脑后提示Target runtime is not defined的解决办法 Eclipse中有一个应用提示一个ErrorTarget runtime xxx is n...
  • littleqqqqq
  • littleqqqqq
  • 2016年10月10日 17:02
  • 1587

Target runtime jdk is not defined 项目上红叉报错解决方法

在Markers窗口上显示Faceted Project Problem  错误,
  • yyhcsfy
  • yyhcsfy
  • 2014年08月14日 10:47
  • 4892
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Changing Target Web Service At Runtime
举报原因:
原因补充:

(最多只允许输入30个字)