动态添加Web 引用

最近在做一个手机客户端项目开发.客户端操作系统是Windows Mobile 6.0 ,服务器端操作系统是:Windows Server 2003.

功能是:获取手机上摄像机或者照片和视频通过网络传回服务器端.

对于图片和视频传输过程,我考虑了两种方法,一种是在用C/S模式,就是客户端和服务器端用Windows Socket传输文件,另一种是在服务器上建立Web Service来接收客户端上传的文件.因为,服务器端最终的应用是建立在Web 程序上的,而且我个人对Socket开发不是很熟悉,所以,先择了用Web Service 的方式.

而应用这种方式要考虑的问题就是程序移植部署时,客户端程序对Web 服务的动态引用问题.

我的实现方法如下:

用Vs.net添加Web引用后,实例化该类,在实例化语句中,右击该类类名,在弹出菜单中选择"转到定义",可以看到类似于以下的代码:

 

ContractedBlock.gif ExpandedBlockStart.gif Code
 1//------------------------------------------------------------------------------
 2// <auto-generated>
 3//     此代码由工具生成。
 4//     运行库版本:2.0.50727.42
 5//
 6//     对此文件的更改可能会导致不正确的行为,并且如果
 7//     重新生成代码,这些更改将会丢失。
 8// </auto-generated>
 9//------------------------------------------------------------------------------
10
11// 
12// 此源代码是由 Microsoft.CompactFramework.Design.Data 2.0.50727.42 版自动生成。
13// 
14ExpandedBlockStart.gifContractedBlock.gifnamespace FileOperation.FileUpload {
15    using System.Diagnostics;
16    using System.Web.Services;
17    using System.ComponentModel;
18    using System.Web.Services.Protocols;
19    using System;
20    using System.Xml.Serialization;
21    
22    
23ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <remarks/>
24    [System.Diagnostics.DebuggerStepThroughAttribute()]
25    [System.ComponentModel.DesignerCategoryAttribute("code")]
26    [System.Web.Services.WebServiceBindingAttribute(Name="FileUploadSoap", Namespace="http://tempuri.org/")]
27ExpandedSubBlockStart.gifContractedSubBlock.gif    public partial class FileUpload : System.Web.Services.Protocols.SoapHttpClientProtocol {
28        
29ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <remarks/>
30ExpandedSubBlockStart.gifContractedSubBlock.gif        public FileUpload() {
31            this.Url = "http://localhost/gpsgoogle/FileUpload.asmx";
32        }

33
34        
35
36ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <remarks/>
37        [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/HelloWorld", RequestNamespace="http://tempuri.org/", ResponseNamespace="http://tempuri.org/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
38ExpandedSubBlockStart.gifContractedSubBlock.gif        public string HelloWorld() {
39            object[] results = this.Invoke("HelloWorld"new object[0]);
40            return ((string)(results[0]));
41        }

42        
43ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <remarks/>
44ExpandedSubBlockStart.gifContractedSubBlock.gif        public System.IAsyncResult BeginHelloWorld(System.AsyncCallback callback, object asyncState) {
45            return this.BeginInvoke("HelloWorld"new object[0], callback, asyncState);
46        }

47        
48ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <remarks/>
49ExpandedSubBlockStart.gifContractedSubBlock.gif        public string EndHelloWorld(System.IAsyncResult asyncResult) {
50            object[] results = this.EndInvoke(asyncResult);
51            return ((string)(results[0]));
52        }

53        
54ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <remarks/>
55        [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/UploadFile", RequestNamespace="http://tempuri.org/", ResponseNamespace="http://tempuri.org/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
56ExpandedSubBlockStart.gifContractedSubBlock.gif        public string UploadFile([System.Xml.Serialization.XmlElementAttribute(DataType="base64Binary")] byte[] data, string fileName) {
57ExpandedSubBlockStart.gifContractedSubBlock.gif            object[] results = this.Invoke("UploadFile"new object[] {
58                        data,
59                        fileName}
);
60            return ((string)(results[0]));
61        }

62        
63ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <remarks/>
64ExpandedSubBlockStart.gifContractedSubBlock.gif        public System.IAsyncResult BeginUploadFile(byte[] data, string fileName, System.AsyncCallback callback, object asyncState) {
65ExpandedSubBlockStart.gifContractedSubBlock.gif            return this.BeginInvoke("UploadFile"new object[] {
66                        data,
67                        fileName}
, callback, asyncState);
68        }

69        
70ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <remarks/>
71ExpandedSubBlockStart.gifContractedSubBlock.gif        public string EndUploadFile(System.IAsyncResult asyncResult) {
72            object[] results = this.EndInvoke(asyncResult);
73            return ((string)(results[0]));
74        }

75    }

76}

77

其中,可以看到该类的构造函数,是一个没有参数的构造函数,其中函数中用默认值会它的Url字段赋值.因些,我们可以给它增加一个带参数的构造函数,其中参数为Web引用的Url.如下

 

ContractedBlock.gif ExpandedBlockStart.gif Code
1public FileUpload(string url)
2ExpandedBlockStart.gifContractedBlock.gif        {
3            this.Url = url;
4        }

 

这样做以后,我们可以用如下语句实例化该类:

 

ContractedBlock.gif ExpandedBlockStart.gif Code
1string url = @"http://192.168.0.55/FileUpload.asmx";
2            FileUpload.FileUpload fileUpload = new FileOperation.FileUpload.FileUpload(url);

 

这样就实现了动态添加Web引用.

转载于:https://www.cnblogs.com/Frontview/archive/2009/03/10/1408025.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值