C#根据WSDL文件生成WebService服务端代码

虽然现在已经进入了.NET FrameWork 4.0的时代,WebService也已经逐渐被淘汰,取而代之的是WCF。

但在工作中难免遇到需要兼容旧版本程序和按照以前的文档进行开发。

 

一般一个已经实现功能的WebService会发布自己的WSDL文件,供客户端调用生成代理类。

 但有时是先有server与client交互的接口定义(WSDL)文件,然后由server和client端分别写程序,一个提供web服务,一个使用web服务。

 最近,我也遇到了这个问题。由于业务方仅提供了WSDL文件并确定了其规范,需要我们开发服务端供调用。

 

1、使用VS2010提供的工具wsdl.exe由WSDL文件生成cs文件

     使用wsdl.exe的/serverInterface选项(或缩写的 /si)指定输入的wsdl文件(注意,如果要转换的wsdl文件中import了其他wsdl文件,则所有文件都应列出,包括使用到的xsd文件也应列出)。输出将是 一个代码文件(默认是C#的,如果需要别的语言,参考MSDN中wsdl.exe的使用说明),其中包含每个 wsdl 绑定的接口。

   

   示例:假设有ServerInterfaceSample.wsdl 

    wsdl.exe /si ServerInterfaceSample.wsdl

    如果使用Service.xsd作为schema文件,则改为

    wsdl.exe /si ServerInterfaceSample.wsdl  Service.xsd

    生成代码如下:

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//------------------------------------------------------------------------------
// <auto-generated>
//     此代码由工具生成。
//     运行时版本:4.0.30319.239
//
//     对此文件的更改可能会导致不正确的行为,并且如果
//     重新生成代码,这些更改将会丢失。
// </auto-generated>
//------------------------------------------------------------------------------
 
using  System;
using  System.ComponentModel;
using  System.Diagnostics;
using  System.Web.Services;
using  System.Web.Services.Protocols;
using  System.Xml.Serialization;
 
//
// 此源代码由 wsdl 自动生成, Version=4.0.30319.1。
//
 
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute( "wsdl" "4.0.30319.1" )]
[System.Web.Services.WebServiceBindingAttribute(Name= "WebServiceSoap" , Namespace= "http://tempuri.org/" )]
public  interface  IWebServiceSoap {
     
     /// <remarks/>
     [System.Web.Services.WebMethodAttribute()]
     [System.Web.Services.Protocols.SoapDocumentMethodAttribute( "http://tempuri.org/sendSMS" , RequestNamespace= "http://tempuri.org/" , ResponseNamespace= "http://tempuri.org/" , Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
     long  sendSMS(InfoHeader header,  string  sessionId,  string  sender,  string  smsContent,  string receiverList,  string  productCode,  string  pseudoFlag);
}
    

 

  2、使用以上生成的文件进行修改,实现你的WebService方法即可.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值