动态添加Web 引用

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

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

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

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

我的实现方法如下:

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

 

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

33InBlock.gif
34InBlock.gif        
35InBlock.gif
36ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <remarks/>
37InBlock.gif        [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() dot.gif{
39InBlock.gif            object[] results = this.Invoke("HelloWorld"new object[0]);
40InBlock.gif            return ((string)(results[0]));
41ExpandedSubBlockEnd.gif        }

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

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

53InBlock.gif        
54ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <remarks/>
55InBlock.gif        [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) dot.gif{
57ExpandedSubBlockStart.gifContractedSubBlock.gif            object[] results = this.Invoke("UploadFile"new object[] dot.gif{
58InBlock.gif                        data,
59ExpandedSubBlockEnd.gif                        fileName}
);
60InBlock.gif            return ((string)(results[0]));
61ExpandedSubBlockEnd.gif        }

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

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

75ExpandedSubBlockEnd.gif    }

76ExpandedBlockEnd.gif}

77None.gif

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

 

ContractedBlock.gifExpandedBlockStart.gifCode
1None.gifpublic FileUpload(string url)
2ExpandedBlockStart.gifContractedBlock.gif        dot.gif{
3InBlock.gif            this.Url = url;
4ExpandedBlockEnd.gif        }

 

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

 

ContractedBlock.gifExpandedBlockStart.gifCode
1None.gifstring url = @"http://192.168.0.55/FileUpload.asmx";
2None.gif            FileUpload.FileUpload fileUpload = new FileOperation.FileUpload.FileUpload(url);

 

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

转载于:https://www.cnblogs.com/lhuser/articles/1508152.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值