根据wsdl生成一个webservice 的.cs文件

本文详细介绍了WebService的两种调用方式,包括直接在Visual Studio中通过Web引用的方式,以及通过命令提示窗口实现服务代理类及动态库文件的生成。同时,还阐述了WCF方法的实现过程,提供了生成WCF代理类的具体步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

webservice的调用方式有两种:

  1. 直接在vs ide中通过web引用的方式,将发布于某个位置的web服务引进到工程里面。这种方式基本上会用vs.net的人都会。
  2. 通过vs 命令提示窗口,命令编译的方式,实现服务代理类(*.cs)及动态库(*.dll)文件的生成,然后在vs中通过引用的方式将生成的文件包括到工程中。

  具体方法:
  打开Microsoft Visual Studio 2005->Visual Studio Tools->Visual Studio 2005 命令提示窗口。
  输入:wsdl ws_file?wsdl /out:WebClient.cs 可生成代理类(ws_file是你的WebService URL,WebClient.cs是你起的代理文件名字) ,
  默认保存路径为C:/Program Files/Microsoft Visual Studio 8/VC/wdstest.cs。 用以上命令生成代理文件,再用csc /t:library WebClient.cs生成dll。

  打开vs自带的命令提示框,直接在vc下输入:

        wsdl /n:xx /out:web.cs http://192.168.0.222/WebSite/Service.asmx?wsdl

        这样,在安装vs的文件夹下的vc文件夹下就会生成你要的web.cs文件。 注:/n 后面表示的是你建立的webserver的命名空间。

 

  WCF方法 

  例如生成WCF代理类:

wsdl /n:xx /out:D:\WCFClient\EtermClient.cs  http://172.17.0.158:1007/eTermService.svc?wsdl

 

 

转载于:https://www.cnblogs.com/jacksun77/p/5033272.html

动态调用WebService的方法类,可以不用在VS本地引用,直接动态调用即可。 样例如下: /// /// 实例化WebServices /// /// WebServices地址 /// 调用的方法 /// 把webservices里需要的参数按顺序放到这个object[]里 public static object InvokeWebService(string url, string methodname, object[] args) { //这里的namespace是需引用的webservices的命名空间,我没有改过,也可以使用。也可以加一个参数从外面传进来。 //string @namespace = "Nq.Application.WebService"; string @namespace = "client"; try { //获取WSDL WebClient wc = new WebClient(); //Stream stream = wc.OpenRead(url); Stream stream = wc.OpenRead(url + "?WSDL"); ServiceDescription sd = ServiceDescription.Read(stream); string classname = sd.Services[0].Name; ServiceDescriptionImporter sdi = new ServiceDescriptionImporter(); sdi.AddServiceDescription(sd, "", ""); CodeNamespace cn = new CodeNamespace(@namespace); //生成客户端代理类代码 CodeCompileUnit ccu = new CodeCompileUnit(); ccu.Namespaces.Add(cn); sdi.Import(cn, ccu); CSharpCodeProvider csc = new CSharpCodeProvider(); //ICodeCompiler icc = csc.CreateCompiler(); //设定编译参数 CompilerParameters cplist = new CompilerParameters(); cplist.GenerateExecutable = false;//动态编译后的程序集不生成可执行文件 cplist.GenerateInMemory = true;//动态编译后的程序集只存在于内存中,不在硬盘的文件上 cplist.ReferencedAssemblies.Add("System.dll"); cplist.ReferencedAssemblies.Add("System.XML.dll"); cplist.ReferencedAssemblies.Add("System.Web.Services.dll"); cplist.ReferencedAssemblies.Add("System.Data.dll"); //编译代理类 CompilerResults cr =
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值