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

转自:http://www.cnblogs.com/liyi93/archive/2012/01/30/2332320.html

虽然现在已经进入了.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方法即可.


好的,我可以为您提供关于 C# WebService 服务端的相关信息。C# WebService 服务端是一种基于 SOAP 协议的 Web 服务,它可以通过 HTTP 或 HTTPS 协议进行通信,实现跨平台的数据传输。下面是一个简单的 C# WebService 服务端的实现示例: 1. 创建一个 C# 类,并在类上添加 [WebService] 和 [WebServiceBinding] 属性,示例代码如下: ``` using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; using System.Web.Services.Protocols; [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class MyWebService : System.Web.Services.WebService { // web service methods here } ``` 2. 在类中添加一个或多个 Web 方法,并使用 [WebMethod] 属性标识它们。示例代码如下: ``` [WebMethod] public string HelloWorld(string name) { return "Hello, " + name + "!"; } ``` 3. 在 Web 服务描述语言 (WSDL) 文件中定义 Web 服务的接口和方法。可以使用 Visual Studio 自带的 WSDL 生成工具或手动创建。示例代码如下: ``` <wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://tempuri.org/" targetNamespace="http://tempuri.org/"> <wsdl:message name="HelloWorldSoapIn"> <wsdl:part name="name" type="xsd:string"/> </wsdl:message> <wsdl:message name="HelloWorldSoapOut"> <wsdl:part name="return" type="xsd:string"/> </wsdl:message> <wsdl:portType name="MyWebServiceSoap"> <wsdl:operation name="HelloWorld"> <wsdl:input message="tns:HelloWorldSoapIn"/> <wsdl:output message="tns:HelloWorldSoapOut"/> </wsdl:operation> </wsdl:portType> <wsdl:binding name="MyWebServiceSoap" type="tns:MyWebServiceSoap"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> <wsdl:operation name="HelloWorld"> <soap:operation soapAction="http://tempuri.org/HelloWorld"/> <wsdl:input> <soap:body use="literal"/> </wsdl:input> <wsdl:output> <soap:body use="literal"/> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:service name="MyWebService"> <wsdl:port name="MyWebServiceSoap" binding="tns:MyWebServiceSoap"> <soap:address location="http://localhost/MyWebService.asmx"/> </wsdl:port> </wsdl:service> </wsdl:definitions> ``` 4. 将该类部署到 IIS 或自承载的 ASP.NET 运行时中。在浏览器中访问 Web 服务的 URL(如 http://localhost/MyWebService.asmx),即可看到 Web 服务的描述信息和测试界面。 希望这些信息能够对您有所帮助。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值