由WSDL檔案產生WEB service server端C#程式

來源:互聯網
上載者:User
http://hi.baidu.com/facile_fengsh/blog/item/d3570eed0de121d5b21cb113.html
一般一個已經實現功能的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)
    {

    }
   
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.