.NET开发

本类阅读TOP10

·NHibernate快速指南(翻译)
·vs.net 2005中文版下载地址收藏
·【小技巧】一个判断session是否过期的小技巧
·VB/ASP 调用 SQL Server 的存储过程
·?dos下编译.net程序找不到csc.exe文件
·通过Web Services上传和下载文件
·学习笔记(补)《.NET框架程序设计(修订版)》--目录
·VB.NET实现DirectDraw9 (2) 动画
·VB.NET实现DirectDraw9 (1) 托管的DDraw
·建站框架规范书之——文件命名

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
Changing Target Web Service At Runtime

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

简介

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



    相关文章

    相关软件