整理-WSDL文件与WEB service
wsdl生成代理类命令
进入 visual studio 命令提示,输入
wsdl.exe /l:CS /n:MathService /out:MathService.cs http://localhost:911/MathService.wsdl
N:命名空间
/out:生成代理类的文件
wsdl是已经做好的webservice
用WSDL和代理类创建可编程WEB服务
在ASP.net中,我们可以创建WSDL文件来描述当前提供的HTML或XML或者任何其他非二进制格式)页,可以使用WSDL来生成客户端代理,并使用Visual Studio.NET或WSDL.exe命令行工具创建代理类。最后通过 RegEx 来分析已命名的HTML页和提取值。以下介绍完整的实现过程:
一、为网站编写WSDL文件
我们以访问http://movies.yahoo.com/电影网站中“本周票房排行榜”(Top Box Office)的数据为例,检索出票房排名第一的影片名称。
通过查看http://movies.yahoo.com/网页的HTML源文件,可以看到排名第一影片的链接是:Finding Nemo,为此可在 WSDL 的响应节中添加 标记。这些标记采用一个称为 pattern 的属性,这是与页面上作为属性值的文本部分相对应的正则表达式。这里我们创建的正规表达式是:“pattern="d=hv&cf=info&id=[0-9]*">(.*?)
<?xml version="1.0" encoding="gb2312"?>
<definitions xmlns:s="http://www.w3.org/2000/10/XMLSchema"
xmlns:http="http://schemas.xmlsoap.org/wsdl/http/"
xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:s0="http://tempuri.org/"
targetNamespace="http://tempuri.org/"
xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:msType="http://microsoft.com/wsdl/mime/textMatching/">
<types/>
<message name="GetBookDetailsHttpGetIn">
<part name="isbn" type="s:string"/>
</message>
<message name="GetBookDetailsHttpGetOut"/>
<portType name="BarnesAndNobleHttpGet">
<operation name="GetBookDetails">
<input message="s0:GetBookDetailsHttpGetIn"/>
<output message="s0:GetBookDetailsHttpGetOut"/>
</operation>
</portType>
<binding name="BarnesAndNobleHttpGet" type="s0:BarnesAndNobleHttpGet">
<http:binding verb="GET"/>
<operation name="GetBookDetails">
<http:operation location=""/>
<input>
<http:urlEncoded/>
</input>
<output>
<msType:text>
<msType:match name="Rank" pattern="d=hv&cf=info&id=[0-9]*">(.*?)
</"ignoreCase="true"/>
</msType:text>
</output>
</operation>
</binding>
<service name="BarnesAndNoble">
<port name="BarnesAndNobleHttpGet" binding="s0:BarnesAndNobleHttpGet">
<http:address location="http://movies.yahoo.com/"/>
</port>
</service>
</definitions>
在上面的WSDL中,定义了BarnesAndNoble类,指定进行检索的站点http://movies.yahoo.com/,由于是一般的通用网站,此服务不使用SOAP,而是使用HTTP GET进行请求。
二、构建WEB服务代理
在Visual Studio.NET中,右键单击“解决方案资源管理器”中的“引用”,选择“添加WEB引用”,就可以打开“添加WEB引用”对话框,
在此对话框中,输入刚才创建好的WSDL文件所在的地址,Visual Studio.NET从指定的位置获取WSDL并验证它。单击“添加引用”按钮,就可以将此WSDL描述的WEB服务的引用添加到当前的工程中。
通过以上操作,Visual Studio.NET在后台自动分析WSDL,并创建了代表Web服务的代理对象,并高速缓存了WSDL的本地副本。如果WSDL内容发生变化,需要手工“更新WEB引用”。
WEB服务代理的源代码如下:
Public Class BarnesAndNoble
Inherits System.Web.Services.Protocols.HttpGetClientProtocol
'<remarks/>
Public Sub New()
MyBase.New
Me.Url = "http://movies.yahoo.com/"
End Sub
'<remarks/>
<System.Web.Services.Protocols.HttpMethodAttribute(GetType
(System.Web.Services.Protocols.TextReturnReader), GetType
(System.Web.Services.Protocols.UrlParameterWriter))> _
Public Function GetBookDetails(ByVal isbn As String)
As GetBookDetailsMatches
Return CType(Me.Invoke("GetBookDetails", (Me.Url + ""),
New Object() {isbn}),GetBookDetailsMatches)
End Function
'<remarks/>
Public Function BeginGetBookDetails(ByVal isbn As String,
ByVal callback As System.AsyncCallback, ByVal asyncState As Object)
As System.IAsyncResult
Return Me.BeginInvoke("GetBookDetails", (Me.Url + ""),
New Object() {isbn}, callback, asyncState)
End Function
'<remarks/>
Public Function EndGetBookDetails(ByVal asyncResult As System.IAsyncResult)
As GetBookDetailsMatches
Return CType(Me.EndInvoke(asyncResult),GetBookDetailsMatches)
End Function
End Class
Public Class GetBookDetailsMatches
<System.Web.Services.Protocols.MatchAttribute("d=hv&cf=info&id=[0-9]*"">
(.*?)</", lgnoreCase:=true)> _
Public Rank As String
End Class
如果在“解决方案资源管理器”中展开“Web References”部分,可以看出具体表达方式:
三、在WEB应用程序中编写代码,使用BarnesAndNoble Web服务。
Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click
Dim bn As New localhost.BarnesAndNoble()
Dim match As localhost.GetBookDetailsMatches
match = bn.GetBookDetails("")
rank.Text = match.Rank
End Sub
在以上程序中,首先调用New localhost.BarnesAndNoble(),创建代理的一个范例bn。bn再调用GetBookDetails()方法传入参数,最后访回一个Rank值(排名第一的影片名称)。
通过编写WSDL,访问由 WSDL 中的功能化名称调用的 Matches 对象,就可以将任何 HTML 部分作为属性来访问,我们可以轻松地将WEB站点转换为WEB服务。以上程序在Windows2000 Server、Visual Studio.NET中调试通过。
由WSDL文件生成WEB service server端C#程序
一、为网站编写WSDL文件
我们以访问http://movies.yahoo.com/电影网站中“本周票房排行榜”(Top Box Office)的数据为例,检索出票房排名第一的影片名称。
通过查看http://movies.yahoo.com/网页的HTML源文件,可以看到排名第一影片的链接是:Finding Nemo,为此可在 WSDL 的响应节中添加 标记。这些标记采用一个称为 pattern 的属性,这是与页面上作为属性值的文本部分相对应的正则表达式。这里我们创建的正规表达式是:“pattern="d=hv&cf=info&id=[0-9]*">(.*?)
<?xml version="1.0" encoding="gb2312"?>
<definitions xmlns:s="http://www.w3.org/2000/10/XMLSchema"
xmlns:http="http://schemas.xmlsoap.org/wsdl/http/"
xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:s0="http://tempuri.org/"
targetNamespace="http://tempuri.org/"
xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:msType="http://microsoft.com/wsdl/mime/textMatching/">
<types/>
<message name="GetBookDetailsHttpGetIn">
<part name="isbn" type="s:string"/>
</message>
<message name="GetBookDetailsHttpGetOut"/>
<portType name="BarnesAndNobleHttpGet">
<operation name="GetBookDetails">
<input message="s0:GetBookDetailsHttpGetIn"/>
<output message="s0:GetBookDetailsHttpGetOut"/>
</operation>
</portType>
<binding name="BarnesAndNobleHttpGet" type="s0:BarnesAndNobleHttpGet">
<http:binding verb="GET"/>
<operation name="GetBookDetails">
<http:operation location=""/>
<input>
<http:urlEncoded/>
</input>
<output>
<msType:text>
<msType:match name="Rank" pattern="d=hv&cf=info&id=[0-9]*">(.*?)
</"ignoreCase="true"/>
</msType:text>
</output>
</operation>
</binding>
<service name="BarnesAndNoble">
<port name="BarnesAndNobleHttpGet" binding="s0:BarnesAndNobleHttpGet">
<http:address location="http://movies.yahoo.com/"/>
</port>
</service>
</definitions>
在上面的WSDL中,定义了BarnesAndNoble类,指定进行检索的站点http://movies.yahoo.com/,由于是一般的通用网站,此服务不使用SOAP,而是使用HTTP GET进行请求。
二、构建WEB服务代理
在Visual Studio.NET中,右键单击“解决方案资源管理器”中的“引用”,选择“添加WEB引用”,就可以打开“添加WEB引用”对话框,
在此对话框中,输入刚才创建好的WSDL文件所在的地址,Visual Studio.NET从指定的位置获取WSDL并验证它。单击“添加引用”按钮,就可以将此WSDL描述的WEB服务的引用添加到当前的工程中。
通过以上操作,Visual Studio.NET在后台自动分析WSDL,并创建了代表Web服务的代理对象,并高速缓存了WSDL的本地副本。如果WSDL内容发生变化,需要手工“更新WEB引用”。
WEB服务代理的源代码如下:
Public Class BarnesAndNoble
Inherits System.Web.Services.Protocols.HttpGetClientProtocol
'<remarks/>
Public Sub New()
MyBase.New
Me.Url = "http://movies.yahoo.com/"
End Sub
'<remarks/>
<System.Web.Services.Protocols.HttpMethodAttribute(GetType
(System.Web.Services.Protocols.TextReturnReader), GetType
(System.Web.Services.Protocols.UrlParameterWriter))> _
Public Function GetBookDetails(ByVal isbn As String)
As GetBookDetailsMatches
Return CType(Me.Invoke("GetBookDetails", (Me.Url + ""),
New Object() {isbn}),GetBookDetailsMatches)
End Function
'<remarks/>
Public Function BeginGetBookDetails(ByVal isbn As String,
ByVal callback As System.AsyncCallback, ByVal asyncState As Object)
As System.IAsyncResult
Return Me.BeginInvoke("GetBookDetails", (Me.Url + ""),
New Object() {isbn}, callback, asyncState)
End Function
'<remarks/>
Public Function EndGetBookDetails(ByVal asyncResult As System.IAsyncResult)
As GetBookDetailsMatches
Return CType(Me.EndInvoke(asyncResult),GetBookDetailsMatches)
End Function
End Class
Public Class GetBookDetailsMatches
<System.Web.Services.Protocols.MatchAttribute("d=hv&cf=info&id=[0-9]*"">
(.*?)</", lgnoreCase:=true)> _
Public Rank As String
End Class
如果在“解决方案资源管理器”中展开“Web References”部分,可以看出具体表达方式:
三、在WEB应用程序中编写代码,使用BarnesAndNoble Web服务。
Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click
Dim bn As New localhost.BarnesAndNoble()
Dim match As localhost.GetBookDetailsMatches
match = bn.GetBookDetails("")
rank.Text = match.Rank
End Sub
在以上程序中,首先调用New localhost.BarnesAndNoble(),创建代理的一个范例bn。bn再调用GetBookDetails()方法传入参数,最后访回一个Rank值(排名第一的影片名称)。
通过编写WSDL,访问由 WSDL 中的功能化名称调用的 Matches 对象,就可以将任何 HTML 部分作为属性来访问,我们可以轻松地将WEB站点转换为WEB服务。以上程序在Windows2000 Server、Visual Studio.NET中调试通过。
由WSDL文件生成WEB service server端C#程序
一般一个已经实现功能的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)
{
}
}
大功告成,在实现函数中加入处理代码就可以了.
但有时是先有的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)
{
}
}
大功告成,在实现函数中加入处理代码就可以了.

浙公网安备 33010602011771号