已有的WSDL文件在VS2005中生成server端代码
已有的WSDL文件在VS2005中生成server端代码收藏
一般一个已经实现功能的WEB Server会发布自己的WSDL文件,供客户端生成代理类。
但有时是先有的server与client交互的接口定义(WSDL)文件,然后由server和client端分别写程序,一个提供web服务,一个使用web服务。
以下介绍如何由已有的WSDL文件在VS2005中生成server端代码。
1)使用VS2005提供的工具wsdl.exe由WSDL文件生成cs文件:
使用wsdl.exe的/serverInterface选项(或缩写的 /si)指定输入的wsdl文件(注意,如果要转换的wsdl文件中import了其他wsdl文件,则所有文件都应列出,包括使用到的xsd文件也应列出)。输出将是 一个代码文件(默认是C#的,如果需要别的语言,参考MSDN中wsdl.exe的使用说明),其中包含每个 wsdl 绑定的接口。
示例:假设ServerInterfaceSample.wsdl文件中import了importedSample.wsdl,并使用Service.xsd作为schema文件;
wsdl.exe /si ServerInterfaceSample.wsdl importedSample.wsdl Service.xsd
生成代码如下:
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:2.0.50727.42
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Serialization;
//
// This source code was auto-generated by wsdl, Version=2.0.50727.42.
//
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "2.0.50727.42")]
[System.Web.Services.WebServiceBindingAttribute(Name="org.csapi.cs.IpChargingManagerSOAPBinding", Namespace="http://www.csapi.org/cs/wsdl")]
public interface IOrgcsapicsIpChargingManagerSOAPBinding {
/// <remarks/>
[System.Web.Services.WebMethodAttribute()]
[System.Web.Services.Protocols.SoapDocumentMethodAttribute("", RequestElementName="org.csapi.cs.IpChargingManager.createChargingSession", RequestNamespace="http://www.csapi.org/cs/schema", ResponseElementName="org.csapi.cs.IpChargingManager.createChargingSessionResult", ResponseNamespace="http://www.csapi.org/cs/schema", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
[return: System.Xml.Serialization.XmlElementAttribute("return", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
orgcsapicsTpChargingSessionID createChargingSession([System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] EndpointReferenceType appChargingSession, [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] string sessionDescription, [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] orgcsapicsTpMerchantAccountID merchantAccount, [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] orgcsapiTpAddress user, [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] orgcsapicsTpCorrelationID correlationID);
/// <remarks/>
[System.Web.Services.WebMethodAttribute()]
[System.Web.Services.Protocols.SoapDocumentMethodAttribute("", RequestElementName="org.csapi.cs.IpChargingManager.createSplitChargingSession", RequestNamespace="http://www.csapi.org/cs/schema", ResponseElementName="org.csapi.cs.IpChargingManager.createSplitChargingSessionResult", ResponseNamespace="http://www.csapi.org/cs/schema", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
[return: System.Xml.Serialization.XmlElementAttribute("return", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
orgcsapicsTpChargingSessionID createSplitChargingSession([System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] EndpointReferenceType appChargingSession, [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] string sessionDescription, [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] orgcsapicsTpMerchantAccountID merchantAccount, [System.Xml.Serialization.XmlArrayAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] [System.Xml.Serialization.XmlArrayItemAttribute("item", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)] orgcsapiTpAddress[] users, [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] orgcsapicsTpCorrelationID correlationID);
/// <remarks/>
[System.Web.Services.WebMethodAttribute()]
[System.Web.Services.Protocols.SoapDocumentMethodAttribute("", RequestElementName="org.csapi.IpService.setCallback", RequestNamespace="http://www.csapi.org/osa/schema", ResponseElementName="org.csapi.IpService.setCallbackResult", ResponseNamespace="http://www.csapi.org/osa/schema", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
void setCallback([System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] EndpointReferenceType appInterface);
/// <remarks/>
[System.Web.Services.WebMethodAttribute()]
[System.Web.Services.Protocols.SoapDocumentMethodAttribute("", RequestElementName="org.csapi.IpService.setCallbackWithSessionID", RequestNamespace="http://www.csapi.org/osa/schema", ResponseElementName="org.csapi.IpService.setCallbackWithSessionIDResult", ResponseNamespace="http://www.csapi.org/osa/schema", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
void setCallbackWithSessionID([System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] EndpointReferenceType appInterface, [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] int sessionID);
}
代码中有一个接口类,并给出了接口函数声明.
此功能的优点是将实现代码和协定代码(如接口所定义的那样)分开。 如果更改 wsdl,然后重新生成接口,则不会丢失任何实现代码。 若要遵循建议的设计指南,则不应将任何代码放置在更改 wsdl 协定的实现中。 同样,不应使用影响运行时行为的代码来更改接口。 协定和实现的详细信息应该分开。
2) 在VS IDE中新建一个web service工程,加入新生成的**Interface.cs文件.
将自动生成的Service类改为从加入的Interface接口类派生,并实现所有的接口函数.(注意名空间一致)
[WebService(Namespace = "http://www.csapi.org/cs/wsdl")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : IOrgcsapicsIpChargingManagerSOAPBinding
{
public orgcsapicsTpChargingSessionID createChargingSession(EndpointReferenceType appChargingSession, string sessionDescription, orgcsapicsTpMerchantAccountID merchantAccount, orgcsapiTpAddress user,orgcsapicsTpCorrelationID correlationID)
{
return new orgcsapicsTpChargingSessionID();
}
public orgcsapicsTpChargingSessionID createSplitChargingSession(EndpointReferenceType appChargingSession, string sessionDescription, orgcsapicsTpMerchantAccountID merchantAccount, orgcsapiTpAddress[] users, orgcsapicsTpCorrelationID correlationID)
{
return new orgcsapicsTpChargingSessionID();
}
public void setCallback(EndpointReferenceType appInterface)
{
}
public void setCallbackWithSessionID(EndpointReferenceType appInterface, int sessionID)
{
}
}
大功告成,在实现函数中加入处理代码就可以了.
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/wei0527/archive/2009/03/15/3991849.aspx