使用ASP
XML Web服務的基礎結構是構建來符合象SOAP
當你使用ASP
重要的是要注意XML Web服務並不能取代DCOM
因為ASP
XML Web服務是一個強大的技術
XML Web服務既支持同步的又支持異步的客戶端和存放XML Web服務的服務器之間的通信
當你使用Web服務描述語言工具(Wsdl
使用異步通信能夠改善系統使用率和避免客戶端在它等待XML Web服務結果的時候的延遲
下面的代碼示例顯示如何從一個客戶應用程序產生一個到XML Web服務的異步調用
[C#]
<%@ Page Language=
<%@ Import Namespace=
<html>
<script language=
void EnterBtn_Click(Object Src
{
MyMath
// Call to Add XML Web service method asynchronously
// and then wait for it to complete
IAsyncResult result =
math
Convert
null
null);
// Wait for asynchronous call to complete
result
// Complete the asynchronous call to Add XML Web service method
float total = math
// Display results in a Label control
Total
}
</script>
<body>
<form action=
<font face=
Enter the two numbers you want to add and then press
the Total button
<p>
Number
<asp:textbox id=
runat=server/>
+
Number
<asp:textbox id=
runat=server/>
=
<asp:button id=
text=
OnClick=
runat=server/>
<p>
<asp:label id=
</font>
</form>
</body>
</html>
[Visual Basic]
<%@ Page Language=
<%@ Import Namespace=
<html>
<script language=
Sub EnterBtn_Click(Src As Object
Dim math As New MyMath
Dim result As IAsyncResult = _
math
Convert
Nothing
Nothing)
result
Dim addtotal As Single = math
Total
End Sub
</script>
<body>
<form action=
<font face=
Enter the two numbers you want to add and then press
the Total button
<p>
Number
<asp:textbox id=
runat=server/>
+
Number
<asp:textbox id=
runat=server/>
=
<asp:button id=
text=
OnClick=
runat=server/>
<p>
<asp:label id=
</font>
</form>
</body>
</html>
想獲得更多關於異步通信的信息
通過因特網產生許多服務請求可能影響客戶應用程序的性能
下面的代碼示例解釋如何把有關信息組織到單個XML Web服務方法中
[C#]
<%@ WebService Language=
using System;
using System
using System
using System
public class DataService {
[WebMethod]
public DataSet GetTitleAuthors() {
SqlConnection myConnection = new SqlConnection(
SqlDataAdapter myCommand
SqlDataAdapter myCommand
DataSet ds = new DataSet();
myCommand
myCommand
return ds;
}
}
[Visual Basic]
<%@ WebService Language=
Imports System
Imports System
Imports System
Imports System
Public Class DataService
<WebMethod> _
Public Function GetTitleAuthors() As DataSet
Dim myConnection As New SqlConnection(
Dim myCommand
Dim myCommand
Dim ds As New DataSet()
myCommand
myCommand
Return ds
End Function
End Class
當設計你的XML Web服務時
下面的代碼示例顯示如何使用繼承來創建一個執行數學計算的XML Web服務
[C#]
<%@ WebService Language=
using System;
using System
abstract public class MathService : WebService
{
[WebMethod]
abstract public float CalculateTotal(float a
}
public class Add : MathService
{
[WebMethod]
override public float CalculateTotal(float a
{
return a + b;
}
}
public class Subtract : MathService
{
[WebMethod]
override public float CalculateTotal(float a
{
return a
}
}
public class Multiply : MathService
{
[WebMethod]
override public float CalculateTotal(float a
{
return a * b;
}
}
public class Divide : MathService
{
[WebMethod]
override public float CalculateTotal(float a
{
if (b==
return
else
return a / b;
}
}
[Visual Basic]
<%@ WebService Language=
Imports System
Imports System
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
Return a + b
End Function
End Class
Public Class Subtract : Inherits MathService
<WebMethod> Public Overrides Function CalculateTotal(a As Single
Return a
End Function
End Class
Public Class Multiply : Inherits MathService
<WebMethod> Public Overrides Function CalculateTotal(a As Single
Return a * b
End Function
End Class
Public Class Divide : Inherits MathService
<WebMethod> Public Overrides Function CalculateTotal(a As Single
If b =
Return
Else
Return a / b
End If
End Function
End Class
使用輸出緩沖來改善你的XML Web服務的性能
在客戶端上使用輸出高速緩沖的指令是
<%@ OutputCache Duration=
下面的代碼示例顯示如何在客戶應用程序上使用Duration屬性來指定輸出高速緩沖為
[C#]
<%@ Page Language=
<%@ Import Namespace=
<%@ OutputCache Duration=
<html>
<script language=
void EnterBtn_Click(Object Src
{
MyMath
// Call the XML Web service
float total = math
// Display the results in a Label control
Total
}
</script>
<body>
<form action=
<font face=
Enter the two numbers you want to add and press
the Total button
<p>
Number
<asp:textbox id=
+ Number
<asp:textbox id=
= <asp:button id=
<p>
<asp:label id=
</font>
</form>
</body>
</html>
[Visual Basic]
<%@ Page Language=
<%@ Import Namespace=
<%@ OutputCache Duration=
<html>
<script language=
Sub EnterBtn_Click(Src As Object
Dim math As New MyMath
Dim addtotal As Single = math
Total
End Sub
</script>
<body>
<form action=
<font face=
Enter the two numbers you want to add and press
the Total button
<p>
Number
<asp:textbox id=
+
Number
<asp:textbox id=
= <asp:button id=
<p>
<asp:label id=
</font>
</form>
</body>
</html>
你還可以使用WebMethod屬性類的CacheDuration屬性來在服務器上允許高速緩沖
[C#]
<%@ WebService Language=
using System;
using System
public class MathService : WebService {
[WebMethod(CacheDuration=
public float Add(float a
{
return a + b;
}
[WebMethod(CacheDuration=
public float Subtract(float a
{
return a
}
[WebMethod(CacheDuration=
public float Multiply(float a
{
return a * b;
}
[WebMethod(CacheDuration=
public float Divide(float a
{
if (b==
return a / b;
}
}
[Visual Basic]
<%@ WebService Language=
Imports System
Imports System
Public Class MathService
Inherits WebService
<WebMethod(CacheDuration :=
Public Function Add(a As Single
Return a + b
End Function
<WebMethod(CacheDuration :=
Public Function Subtract(a As Single
Return a
End Function
<WebMethod(CacheDuration :=
Public Function Multiply(a As Single
Return a * b
End Function
<WebMethod(CacheDuration :=
Public Function Divide(a As Single
If b =
Return
End If
Return a / b
End Function
End Class
當設計你的XML Web服務時
XML Web服務使用SOAP作為主要的傳送和序列化協議
一個SOAP消息由一個可選擇的頭體和消息體組成
提供用於你的XML Web服務的文檔
From:http://tw.wingwit.com/Article/program/net/201311/11884.html