ASP.NET創(chuàng)建XML Web服務(wù)全接觸(7)
發(fā)表時間:2024-02-13 來源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]通過因特網(wǎng)產(chǎn)生許多服務(wù)請求可能影響客戶應(yīng)用程序的性能。當(dāng)設(shè)計你的XML Web服務(wù)時,通過創(chuàng)建把有關(guān)信息集中在一起的方法可以有效利用服務(wù)請求。例如,假定你有一個XML Web服務(wù),用來檢索一本書的信息。我們可以創(chuàng)建一個在一條服務(wù)請求中返回所有的信息的方法,來代替單獨的檢索書名、作者和出版社的方法。...
通過因特網(wǎng)產(chǎn)生許多服務(wù)請求可能影響客戶應(yīng)用程序的性能。當(dāng)設(shè)計你的XML Web服務(wù)時,通過創(chuàng)建把有關(guān)信息集中在一起的方法可以有效利用服務(wù)請求。例如,假定你有一個XML Web服務(wù),用來檢索一本書的信息。我們可以創(chuàng)建一個在一條服務(wù)請求中返回所有的信息的方法,來代替單獨的檢索書名、作者和出版社的方法。一次傳送大塊的信息比多次傳送小塊的信息更有效率。
下面的代碼示例解釋如何把有關(guān)信息組織到單個XML Web服務(wù)方法中。 [C#] <%@ WebService Language="C#" Class="DataService" %> using System; using System.Data; using System.Data.SqlClient; using System.Web.Services; public class DataService { [WebMethod] public DataSet GetTitleAuthors() { SqlConnection myConnection = new SqlConnection("Persist Security Info=False;Integrated Security=SSPI;server=localhost;database=pubs"); SqlDataAdapter myCommand1 = new SqlDataAdapter ("select * from Authors", myConnection); SqlDataAdapter myCommand2 = new SqlDataAdapter("select * from Titles", myConnection); DataSet ds = new DataSet(); myCommand1.Fill(ds, "Authors"); myCommand2.Fill(ds, "Titles"); return ds; } } [Visual Basic] <%@ WebService Language="VB" Class="DataService" %> Imports System Imports System.Data Imports System.Data.SqlClient Imports System.Web.Services Public Class DataService <WebMethod> _ Public Function GetTitleAuthors() As DataSet Dim myConnection As New SqlConnection("Persist Security Info=False;Integrated Security=SSPI;server=localhost;database=pubs") Dim myCommand1 As New SqlDataAdapter("select * from Authors", myConnection) Dim myCommand2 As New SqlDataAdapter("select * from Titles", myConnection) Dim ds As New DataSet() myCommand1.Fill(ds, "Authors") myCommand2.Fill(ds, "Titles") Return ds End Function End Class |
當(dāng)設(shè)計你的XML Web服務(wù)時,請確保使用標(biāo)準(zhǔn)的面向?qū)ο缶幊滩僮鳌J褂梅庋b來隱藏實現(xiàn)細(xì)節(jié)。對于更復(fù)雜的XML Web服務(wù),你可以使用繼承和多態(tài)性來再次使用代碼并簡化你的設(shè)計。
下面的代碼示例顯示如何使用繼承來創(chuàng)建一個執(zhí)行數(shù)學(xué)計算的XML Web服務(wù)。
[C#] <%@ WebService Language="C#" Class="Add" %> using System; using System.Web.Services; abstract public class MathService : WebService { [WebMethod] abstract public float CalculateTotal(float a, float b); } public class Add : MathService { [WebMethod] override public float CalculateTotal(float a, float b) { return a + b; } } public class Subtract : MathService { [WebMethod] override public float CalculateTotal(float a, float b) { return a - b; } } public class Multiply : MathService { [WebMethod] override public float CalculateTotal(float a, float b) { return a * b; } } public class Divide : MathService { [WebMethod] override public float CalculateTotal(float a, float b) { if (b==0) return -1; else return a / b; } } [Visual Basic] <%@ WebService Language="VB" Class="Add" %> Imports System Imports System.Web.Services MustInherit Public Class MathService : Inherits WebService <WebMethod> _ Public MustOverride Function CalculateTotal(a As Single, _ b As Single) As Single End Class Public Class Add : Inherits MathService <WebMethod> Public Overrides Function CalculateTotal(a As Single, b As Single) As Single Return a + b End Function End Class Public Class Subtract : Inherits MathService <WebMethod> Public Overrides Function CalculateTotal(a As Single, b As Single) As Single Return a - b End Function End Class Public Class Multiply : Inherits MathService <WebMethod> Public Overrides Function CalculateTotal(a As Single, b As Single) As Single Return a * b End Function End Class Public Class Divide : Inherits MathService <WebMethod> Public Overrides Function CalculateTotal(a As Single, b As Single) As Single If b = 0 Then Return - 1 Else Return a / b End If End Function End Class |