动态添加Web 引用

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

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

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

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

我的实现方法如下:

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

 


 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// 
14namespace 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    
23    /// <remarks/>
24    [System.Diagnostics.DebuggerStepThroughAttribute()]
25    [System.ComponentModel.DesignerCategoryAttribute("code")]
26    [System.Web.Services.WebServiceBindingAttribute(Name="FileUploadSoap", Namespace="http://tempuri.org/")]
27    public partial class FileUpload : System.Web.Services.Protocols.SoapHttpClientProtocol {
28        
29        /// <remarks/>
30        public FileUpload() {
31            this.Url = "http://localhost/gpsgoogle/FileUpload.asmx";
32        }

33
34        
35
36        /// <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)]
38        public string HelloWorld() {
39            object[] results = this.Invoke("HelloWorld"new object[0]);
40            return ((string)(results[0]));
41        }

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

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

53        
54        /// <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)]
56        public string UploadFile([System.Xml.Serialization.XmlElementAttribute(DataType="base64Binary")] byte[] data, string fileName) {
57            object[] results = this.Invoke("UploadFile"new object[] {
58                        data,
59                        fileName}
);
60            return ((string)(results[0]));
61        }

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

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

75    }

76}

77

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

 


1public FileUpload(string url)
2        {
3            this.Url = url;
4        }

 

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

 


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

 

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

posted @ 2009-06-22 11:16  Daniel_Lu  阅读(645)  评论(0编辑  收藏  举报