使用ASP.NET構(gòu)造一個XML Web服務(wù)
使用ASP.NET構(gòu)造一個簡單的XML Web服務(wù)是相對容易的,然而,XML Web服務(wù)的真正的強大的功能只有等你研究了基礎(chǔ)結(jié)構(gòu)以后才能領(lǐng)悟。XML Web服務(wù)是建立在.NET框架和公共語言運行時間基礎(chǔ)上的。一個XML Web服務(wù)可以利用這些技術(shù)。例如,ASP.NET支持的性能、狀態(tài)管理和驗證全都可被用來構(gòu)造XML Web服務(wù)。
XML Web服務(wù)的基礎(chǔ)結(jié)構(gòu)是構(gòu)建來符合象SOAP、XML和WSDL這樣的行業(yè)標(biāo)準(zhǔn),并且它允許其他的平臺的客戶端與XML Web服務(wù)互操作。只要一個客戶端可以發(fā)送符合標(biāo)準(zhǔn)的SOAP消息、依據(jù)格式化的服務(wù)描述,那么這個客戶端可以調(diào)用一個使用ASP.NET創(chuàng)建的XML Web服務(wù)(不管客戶端存在于何種平臺)。
當(dāng)你使用ASP.NET構(gòu)造一個XML Web服務(wù)時,它自動支持客戶端使用SOAP、HTTP-GET和HTTP-POST協(xié)議通訊。即使HTTP-GET和HTTP-POST支持使用URL編碼的變量名/變量值對來傳送消息,支持這兩個協(xié)議的數(shù)據(jù)類型也沒有支持SOAP協(xié)議的數(shù)據(jù)類型豐富。在SOAP中,使用XML把數(shù)據(jù)傳送到XML Web服務(wù)或從XML Web服務(wù)取回消息,你可以使用支持豐富的數(shù)據(jù)類型集的XSD模式定義復(fù)雜的數(shù)據(jù)類型。使用ASP.NET構(gòu)造一個XML Web服務(wù)的開發(fā)者不必明確地定義復(fù)雜的數(shù)據(jù)類型。他們可以只構(gòu)造一個管理類。ASP.NET處理定義到一個XSD模式的映射類和到XML數(shù)據(jù)的映射對象實例,以便通過網(wǎng)絡(luò)傳輸。
重要的是要注意XML Web服務(wù)并不能取代DCOM,我們應(yīng)該說XML Web服務(wù)是跨越使用行業(yè)標(biāo)準(zhǔn)的平臺通信的一種消息傳遞基礎(chǔ)結(jié)構(gòu)。
因為ASP.NET提供了為XML Web服務(wù)內(nèi)部工作的基礎(chǔ)結(jié)構(gòu),開發(fā)者可以集中精力來實現(xiàn)他們的特定的XML Web服務(wù)的功能。使用ASP.NET開發(fā)一個XML Web服務(wù)從下面三步開始:
1. 創(chuàng)建一個帶有.asmx擴展名的文件。
2. 在這個文件里面,使用一條指令聲明XML Web服務(wù)。
3. 定義組成XML Web服務(wù)功能的XML Web服務(wù)方法。
XML Web服務(wù)是一個強大的技術(shù),用于提供可通過因特網(wǎng)變成訪問的服務(wù)。下面的建議將幫助你創(chuàng)建高效的XML Web服務(wù):
XML Web服務(wù)既支持同步的又支持異步的客戶端和存放XML Web服務(wù)的服務(wù)器之間的通信。在同步通信情況下,客戶端發(fā)送一個對服務(wù)的請求到服務(wù)主機服務(wù)器上等待響應(yīng)。這防止客戶端在等待結(jié)果的時候,執(zhí)行其它的操作。然而異步通信導(dǎo)致客戶端在等待相應(yīng)的時候繼續(xù)處理其它的任務(wù)??蛻舳嗽诳捎玫臅r候響應(yīng)服務(wù)請求的結(jié)果。
當(dāng)你使用Web服務(wù)描述語言工具(Wsdl.exe)來創(chuàng)建你的代理類的時候,它產(chǎn)生類中的方法的標(biāo)準(zhǔn)的、同步版本和異步版本。異步版本由兩個方法組成,稱為Begin和End。Begin方法用于初始化XML Web服務(wù),而End方法取得結(jié)果。
使用異步通信能夠改善系統(tǒng)使用率和避免客戶端在它等待XML Web服務(wù)結(jié)果的時候的延遲。
下面的代碼示例顯示如何從一個客戶應(yīng)用程序產(chǎn)生一個到XML Web服務(wù)
- <%@ Page Language="C#" %>
- <%@ Import Namespace="System.Net" %>
- <html>
- <script language="C#" runat="server">
- void EnterBtn_Click(Object Src, EventArgs E)
- {
- MyMath.Math math = new MyMath.Math();
- // Call to Add XML Web service method asynchronously
- // and then wait for it to complete.
- IAsyncResult result =
- math.BeginAdd(Convert.ToInt32(Num1.Text),
- Convert.ToInt32(Num2.Text),
- null,
- null);
- // Wait for asynchronous call to complete.
- result.AsyncWaitHandle.WaitOne();
- // Complete the asynchronous call to Add XML Web service method.
- float total = math.EndAdd(result);
- // Display results in a Label control.
- Total.Text = "Total: " + total.ToString();
- }
- </script>
- <body>
- <form action="MathClient.aspx" runat=server>
- <font face="Verdana">
- Enter the two numbers you want to add and then press
- the Total button.
- <p>
- Number 1:
- <asp:textbox id="Num1"
- runat=server/>
- +
- Number 2:
- <asp:textbox id="Num2"
- runat=server/>
- =
- <asp:button id="Total_Button"
- text="Total"
- OnClick="EnterBtn_Click"
- runat=server/>
- <p>
- <asp:label id="Total" runat=server/>
- </font>
- </form>
- </body>
- </html>
- [Visual Basic]
- <%@ Page Language="VB" %>
- <%@ Import Namespace="System.Net" %>
- <html>
- <script language="VB" runat="server">
- Sub EnterBtn_Click(Src As Object, E As EventArgs)
- Dim math As New MyMath.Math()
- ' Call to Add XML Web service method asynchronously
- ' and then wait for it to complete.
- Dim result As IAsyncResult = _
- math.BeginAdd(Convert.ToInt32(Num1.Text), _
- Convert.ToInt32(Num2.Text), _
- Nothing, _
- Nothing)
- ' Wait for asynchronous call to complete.
- result.AsyncWaitHandle.WaitOne()
- ' Complete the asynchronous call to Add XML Web service method.
- Dim addtotal As Single = math.EndAdd(result)
- ' Display results in a Label control.
- Total.Text = "Total: " & addtotal.ToString()
- End Sub
- </script>
- <body>
- <form action="MathClient.aspx" runat=server>
- <font face="Verdana">
- Enter the two numbers you want to add and then press
- the Total button.
- <p>
- Number 1:
- <asp:textbox id="Num1"
- runat=server/>
- +
- Number 2:
- <asp:textbox id="Num2"
- runat=server/>
- =
- <asp:button id="Total_Button"
- text="Total"
- OnClick="EnterBtn_Click"
- runat=server/>
- <p>
- <asp:label id="Total" runat=server/>
- </font>
- </form>
- </body>
- </html>
【編輯推薦】