CSDN博客

img qieyj

Changing Target Web Service At Runtime

发表于2003/4/8 9:05:00  1060人阅读

分类: .NET

简介

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

    相关博文

    我的热门文章

    img
    取 消
    img