利用wsdl.exe自动将wsdl文档转换为C#代码

1.获取完整的wsdl文档

获取下面这个博客中提到的wsdl

 http://www.cnblogs.com/LCCRNblog/p/3716406.html

将获取到的wsdl放到一个文本中,改后缀(我放到mywsdl.wsdl),然后将wsdl文档放到桌面一个wsdl文件夹。

2.找到wsdl.exe

wsdl.exe一般在C:Program FilesMicrosoft SDKsWindowsv7.0Ain中,找到它后在环境变量中添加这个路径。

3.使用wsdl.exe

打开cmd。

可以输入:wsdl /?,理解wsdl命令的参数;

接下来就是怎样获取C#代码的过程。

输入命令:wsdl.exe /si C:UsersLCDesktopwsdlmywsdl.wsdl /out:C:UsersLCDesktopwsdl

/si 和 /out:(冒号不能少,我第一次做的时候就是少了冒号,就不成功)这两个参数的意思可以查看刚才wsdl的参数列表,

/si 后跟wsdl所在路径

/out: 后跟生成C#文件的存放路径

生成的文件名为ServiceInterfaces.cs

内容如下:

 1 //------------------------------------------------------------------------------
 2 // <auto-generated>
 3 //     此代码由工具生成。
 4 //     运行时版本:2.0.50727.5477
 5 //
 6 //     对此文件的更改可能会导致不正确的行为,并且如果
 7 //     重新生成代码,这些更改将会丢失。
 8 // </auto-generated>
 9 //------------------------------------------------------------------------------
10 
11 using System;
12 using System.ComponentModel;
13 using System.Diagnostics;
14 using System.Web.Services;
15 using System.Web.Services.Protocols;
16 using System.Xml.Serialization;
17 
18 // 
19 // 此源代码由 wsdl 自动生成, Version=2.0.50727.3038。
20 // 
21 
22 
23 /// <remarks/>
24 [System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "2.0.50727.3038")]
25 [System.Web.Services.WebServiceBindingAttribute(Name="ServiceSoap", Namespace="http://tempuri.org/")]
26 public interface IServiceSoap {
27     
28     /// <remarks/>
29     [System.Web.Services.WebMethodAttribute()]
30     [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/addition", RequestNamespace="http://tempuri.org/", ResponseNamespace="http://tempuri.org/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
31     double addition(double i, double j);
32     
33     /// <remarks/>
34     [System.Web.Services.WebMethodAttribute()]
35     [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/subtract", RequestNamespace="http://tempuri.org/", ResponseNamespace="http://tempuri.org/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
36     double subtract(double i, double j);
37     
38     /// <remarks/>
39     [System.Web.Services.WebMethodAttribute()]
40     [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/mutiplication", RequestNamespace="http://tempuri.org/", ResponseNamespace="http://tempuri.org/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
41     double mutiplication(double i, double j);
42     
43     /// <remarks/>
44     [System.Web.Services.WebMethodAttribute()]
45     [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/division", RequestNamespace="http://tempuri.org/", ResponseNamespace="http://tempuri.org/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
46     double division(double i, double j);
47 }

在此可以和之前那个博客中的代码进行比较一下,看看他们之间的区别。

原文地址:https://www.cnblogs.com/LCCRNblog/p/3717702.html