动态引用Web Service

3 篇文章 0 订阅
1 篇文章 0 订阅

      在项目中添加引用webservice是相对固定的,当webservice更换地址后需要将整个项目重新添加webservice引用,而动态配置则可以在webservice地址发生改变时只改动配置文件即可实现整个项目的webservice的引用的地址的更改,方便了操作。特别是在程序开发测试阶段,可以先用本机的webservice测试,在测试成功后再发布到服务器上。具体的实现步骤如下:

1、添加本机的webservice引用

添加后的图片:

在由引用webservice后,双击既可看到系统自动添加的代码,如:

     [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Web.Services.WebServiceBindingAttribute(Name="GlobalWeatherSoap", Namespace="http://www.webserviceX.NET")]
    public partial class GlobalWeather : System.Web.Services.Protocols.SoapHttpClientProtocol {
        
        /// <remarks/>
        public GlobalWeather() {
            this.Url = "http://www.webservicex.net/globalweather.asmx";
        }
 }

动态引用webservice就是继承系统自动添加的引用,并且将其中的url换位参数传递,默认的没有参数的就是调用引用时的代码,在更换webservice地址后需要引用带参数的相应的函数,这样既可以直接调试我们的系统,又可以在webservice地址发生改变时加以转换。

2、生成动态引用的继承类

需要引用的单元:

using System.Diagnostics;
using System.ComponentModel;
using System.Web.Services;

需要生成的继承类:

    /// <summary>
    /// 动态引用webservice
    /// </summary>   
    [DebuggerStepThrough(), DesignerCategory("code"), WebServiceBinding(Name = "", Namespace = "")]
    class Dyn_Weather : WeatherService.GlobalWeather
    {
        internal Dyn_Weather()
            : base()
        {
            this.Url = "http://www.webservicex.net/globalweather.asmx";
        }

        internal Dyn_Weather(string ip)     //带参数的构造函数,在调用时制定ip地址
            : base()
        {
            this.Url=string.Format("http://{0}/globalweather.asmx",ip);
        }
    }



 3、调用动态webservice

           Dyn_Weather dy = new Dyn_Weather("192.168.1.10");//制定webservice的ip地址
            
            lblDeviceid.Text =dy.GetWeather("beijing", "china").ToString();

      这样只需在构造函数时制定webservice服务的ip地址就可以实现webservice的动态配置,对于项目开发带来了方便性。


 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mushroom_lb

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值