打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
动态调用WebService的方法

通过在"Web引用"中的本地代理类中添加一个构造函数,这个构造函数是以Web Service的URL为参数的重载方法实现动态调用WebService

在Visual Studio中添加Web Service引用时,会生成一个Web References方件夹, 显示这个文件夹中的所有文件,我们会发现Reference.cs文件,打开这个文件,里面定义了一个从System.Web.Services.Protocols.SoapHttpClientProtocol继承的类,在它的构造函数中,有这样的初始化语句: 

this.Url = http://localhost/webservice/MessageService.asmx;

 

我们只要增加一个带有参数的构造函数,参数就是我们要引用的Web Service的url, 就可以实现动态添加对Web Service的引用。

对于ASP.net 1.1
  1.     [System.Diagnostics.DebuggerStepThroughAttribute()]      
  2.     [System.ComponentModel.DesignerCategoryAttribute("code")]      
  3.     [System.Web.Services.WebServiceBindingAttribute(Name="MessageServiceSoap", Namespace="http://tempuri.org/")]      
  4.     public class MessageService : System.Web.Services.Protocols.SoapHttpClientProtocol {      
  5.         public MessageService(string serviceAddress) {      
  6.             this.Url = "http://localhost/webservice/MessageService.asmx";      
  7.         }      
  8.   
  9.         public MessageService(string serviceAddress) {      
  10.             this.Url = serviceAddress;      
  11.         }      
  12. }   
对于ASP.net 2.0
  1.     [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Web.Services""2.0.50727.42")]   
  2.     [System.Diagnostics.DebuggerStepThroughAttribute()]   
  3.     [System.ComponentModel.DesignerCategoryAttribute("code")]   
  4.     [System.Web.Services.WebServiceBindingAttribute(Name="MessageServiceSoap", Namespace="http://tempuri.org/")]   
  5.     public partial class MessageService : System.Web.Services.Protocols.SoapHttpClientProtocol {   
  6.         private System.Threading.SendOrPostCallback RegisterUserOperationCompleted;   
  7.         ……   
  8.         private System.Threading.SendOrPostCallback SendReplyMessageOperationCompleted;   
  9.         private bool useDefaultCredentialsSetExplicitly;   
  10.         public MessageService() {   
  11.             this.Url = "http://localhost:8023/webservice/MessageService.asmx";   
  12.             if ((this.IsLocalFileSystemWebService(this.Url) == true)) {   
  13.                 this.UseDefaultCredentials = true;   
  14.                 this.useDefaultCredentialsSetExplicitly = false;   
  15.             }   
  16.             else {   
  17.                 this.useDefaultCredentialsSetExplicitly = true;   
  18.             }   
  19.         }   
  20.   
  21.         public MessageService(string msurl)   
  22.         {   
  23.             this.Url = msurl;   
  24.             if ((this.IsLocalFileSystemWebService(this.Url) == true))   
  25.             {   
  26.                 this.UseDefaultCredentials = true;   
  27.                 this.useDefaultCredentialsSetExplicitly = false;   
  28.             }   
  29.             else  
  30.             {   
  31.                 this.useDefaultCredentialsSetExplicitly = true;   
  32.             }   
  33.         }   
  34. }  
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
c#webservice的简单示例
使?用?C?#?创?建?w?e?b?s?e?r?v?i?c?e?及?三?种?调?用?方?式
关于动态引用WEB SERVICE的问题
动态加载Web Service
C#动态调用Web服务的3种方法 - 51CTO.COM
学会使用Web Service上(服务器端访问)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服