為了改善調(diào)用阻礙線程的長(zhǎng)期運(yùn)行的方法的XML Web服務(wù)方法的性能,你應(yīng)該考慮把它們作為異步的XML Web服務(wù)方法發(fā)布。實(shí)現(xiàn)一個(gè)異步XML Web服務(wù)方法允許線程在返回線程池的時(shí)候執(zhí)行其他的代碼。這允許增加一個(gè)線程池中的有限數(shù)目的線程,這樣提高了整體性能和系統(tǒng)的可伸縮性。
通常,調(diào)用執(zhí)行輸入/輸出操作的方法的XML Web服務(wù)方法適于作為異步實(shí)現(xiàn)。這樣的方法的例子包括和其他的XML Web服務(wù)通訊、訪問(wèn)遠(yuǎn)程數(shù)據(jù)庫(kù)、執(zhí)行網(wǎng)絡(luò)輸入/輸出和讀寫大文件方法。這些方法都花費(fèi)大量時(shí)間執(zhí)行硬件級(jí)操作,而把線程留著用來(lái)執(zhí)行XML Web服務(wù)方法程序塊。如果XML Web服務(wù)方法異步實(shí)現(xiàn),那么線程可以被釋放來(lái)執(zhí)行其他的代碼。
不管一個(gè)XML Web服務(wù)方法是否異步實(shí)現(xiàn),客戶端都可以與之異步通訊。使用Web服務(wù)描述語(yǔ)言工具(WSDL.EXE)生成的.net客戶端中的代理類來(lái)實(shí)現(xiàn)異步通信,即使XML Web服務(wù)方法是同步實(shí)現(xiàn)。代理類包含用于與每個(gè)XML Web服務(wù)方法異步通信的Begin和End方法。因此,決定一個(gè)XML Web服務(wù)方法到底是異步還是同步要取決于性能。
注意:實(shí)現(xiàn)一個(gè)異步的XML Web服務(wù)方法對(duì)客戶端和服務(wù)器上的XML Web服務(wù)之間的HTTP連接沒(méi)有影響。HTTP連接既不不會(huì)關(guān)閉也不用連接池化。
實(shí)現(xiàn)一個(gè)異步的XML Web服務(wù)方法
實(shí)現(xiàn)一個(gè)異步的XML Web服務(wù)方法遵循NET Framework異步設(shè)計(jì)模式
把一個(gè)同步的XML Web服務(wù)方法分解為兩個(gè)方法;其中每個(gè)都帶有相同的基名--一個(gè)帶有以Begin開(kāi)頭的名稱,另一個(gè)帶有以End開(kāi)頭的名稱。
Begin方法的參數(shù)表包含方法的功能中的所有的in和by引用參數(shù)。
By引用參數(shù)是作為輸入?yún)?shù)列出的。
倒數(shù)第二個(gè)參數(shù)必須是AsyncCallback。AsyncCallback參數(shù)允許客戶端提供一個(gè)委托,在方法調(diào)用完成的時(shí)候調(diào)用。當(dāng)一個(gè)異步XML Web服務(wù)方法調(diào)用另一個(gè)異步方法,這個(gè)參數(shù)可以被傳入那個(gè)方法的倒數(shù)第二個(gè)參數(shù)。最后一個(gè)參數(shù)是一個(gè)對(duì)象。對(duì)象參數(shù)允許一個(gè)調(diào)用者提供狀態(tài)信息給方法。當(dāng)一個(gè)異步XML Web服務(wù)方法調(diào)用另一個(gè)異步方法,這個(gè)參數(shù)可以被傳入那個(gè)方法的最后一個(gè)參數(shù)。
返回值必須是IAsyncResult類型的。
下面的代碼示例是一個(gè)Begin方法,有一個(gè)方法函數(shù)特定的String參數(shù)。
[C#]
[WebMethod]
public IAsyncResult BeginGetAuthorRoyalties(String Author,
AsyncCallback callback, object asyncState)
[Visual Basic]
<WebMethod()> _
Public Function BeginGetAuthorRoyalties(ByVal Author As String, _
ByVal callback As AsyncCallback, ByVal asyncState As Object) _
As IAsyncResult
End方法的參數(shù)表由一個(gè)IAsyncResult類型的out和by引用參數(shù)組成。
返回值與一個(gè)同步的XML Web服務(wù)方法的返回值類型相同。
By引用參數(shù)是作為輸出參數(shù)列出的。
下面的代碼示例是一個(gè)End方法,返回一個(gè)AuthorRoyalties用戶定義的模式。
[C#]
[WebMethod]
public AuthorRoyalties EndGetAuthorRoyalties(IAsyncResult
asyncResult)
[Visual Basic]
<WebMethod()> _
Public Function EndGetAuthorRoyalties(ByVal asyncResult As _
IAsyncResult) As AuthorRoyalties
下面的代碼示例是一個(gè)和另一個(gè)XML Web服務(wù)方法異步通訊的異步XML Web服務(wù)方法。
[C#]
using System;
using System.Web.Services;
[WebService(Namespace="http://www.contoso.com/")]
public class MyService : WebService {
public RemoteService remoteService;
public MyService() {
// Create a new instance of proxy class for
// the XML Web Service to be called.
remoteService = new RemoteService();
}
// Define the Begin method.
[WebMethod]
public IAsyncResult BeginGetAuthorRoyalties(String Author,AsyncCallback callback, object asyncState) {
// Begin asynchronous communictation with a different XML Web
// service.
return remoteService.BeginReturnedStronglyTypedDS(Author,
callback,asyncState);
}
// Define the End method.
[WebMethod]
public AuthorRoyalties EndGetAuthorRoyalties(IAsyncResultasyncResult) {
// Return the asynchronous result from the other XML Web service.
return remoteService.EndReturnedStronglyTypedDS(asyncResult);
}
}
[Visual Basic]
Imports System.Web.Services
<WebService(Namespace:="http://www.contoso.com/")> _
Public Class MyService
Inherits WebService
Public remoteService As RemoteService
Public Sub New()
MyBase.New()
' Create a new instance of proxy class for
' the XML Web service to be called.
remoteService = New RemoteService()
End Sub
' Define the Begin method.
<WebMethod()> _
Public Function BeginGetAuthorRoyalties(ByVal Author As String, _
ByVal callback As AsyncCallback, ByVal asyncState As Object) _
As IAsyncResult
' Begin asynchronous communictation with a different XML Web
' service.
Return remoteService.BeginReturnedStronglyTypedDS(Author, _
callback, asyncState)
End Function
' Define the End method.
<WebMethod()> _
Public Function EndGetAuthorRoyalties(ByVal asyncResult As _
IAsyncResult) As AuthorRoyalties
' Return the asynchronous result from the other XML Web service.
Return remoteService.EndReturnedStronglyTypedDS(asyncResult)
End Function
End Class
通常,調(diào)用執(zhí)行輸入/輸出操作的方法的XML Web服務(wù)方法適于作為異步實(shí)現(xiàn)。這樣的方法的例子包括和其他的XML Web服務(wù)通訊、訪問(wèn)遠(yuǎn)程數(shù)據(jù)庫(kù)、執(zhí)行網(wǎng)絡(luò)輸入/輸出和讀寫大文件方法。這些方法都花費(fèi)大量時(shí)間執(zhí)行硬件級(jí)操作,而把線程留著用來(lái)執(zhí)行XML Web服務(wù)方法程序塊。如果XML Web服務(wù)方法異步實(shí)現(xiàn),那么線程可以被釋放來(lái)執(zhí)行其他的代碼。
不管一個(gè)XML Web服務(wù)方法是否異步實(shí)現(xiàn),客戶端都可以與之異步通訊。使用Web服務(wù)描述語(yǔ)言工具(WSDL.EXE)生成的.net客戶端中的代理類來(lái)實(shí)現(xiàn)異步通信,即使XML Web服務(wù)方法是同步實(shí)現(xiàn)。代理類包含用于與每個(gè)XML Web服務(wù)方法異步通信的Begin和End方法。因此,決定一個(gè)XML Web服務(wù)方法到底是異步還是同步要取決于性能。
注意:實(shí)現(xiàn)一個(gè)異步的XML Web服務(wù)方法對(duì)客戶端和服務(wù)器上的XML Web服務(wù)之間的HTTP連接沒(méi)有影響。HTTP連接既不不會(huì)關(guān)閉也不用連接池化。
實(shí)現(xiàn)一個(gè)異步的XML Web服務(wù)方法
實(shí)現(xiàn)一個(gè)異步的XML Web服務(wù)方法遵循NET Framework異步設(shè)計(jì)模式
把一個(gè)同步的XML Web服務(wù)方法分解為兩個(gè)方法;其中每個(gè)都帶有相同的基名--一個(gè)帶有以Begin開(kāi)頭的名稱,另一個(gè)帶有以End開(kāi)頭的名稱。
Begin方法的參數(shù)表包含方法的功能中的所有的in和by引用參數(shù)。
By引用參數(shù)是作為輸入?yún)?shù)列出的。
倒數(shù)第二個(gè)參數(shù)必須是AsyncCallback。AsyncCallback參數(shù)允許客戶端提供一個(gè)委托,在方法調(diào)用完成的時(shí)候調(diào)用。當(dāng)一個(gè)異步XML Web服務(wù)方法調(diào)用另一個(gè)異步方法,這個(gè)參數(shù)可以被傳入那個(gè)方法的倒數(shù)第二個(gè)參數(shù)。最后一個(gè)參數(shù)是一個(gè)對(duì)象。對(duì)象參數(shù)允許一個(gè)調(diào)用者提供狀態(tài)信息給方法。當(dāng)一個(gè)異步XML Web服務(wù)方法調(diào)用另一個(gè)異步方法,這個(gè)參數(shù)可以被傳入那個(gè)方法的最后一個(gè)參數(shù)。
返回值必須是IAsyncResult類型的。
下面的代碼示例是一個(gè)Begin方法,有一個(gè)方法函數(shù)特定的String參數(shù)。
[C#]
[WebMethod]
public IAsyncResult BeginGetAuthorRoyalties(String Author,
AsyncCallback callback, object asyncState)
[Visual Basic]
<WebMethod()> _
Public Function BeginGetAuthorRoyalties(ByVal Author As String, _
ByVal callback As AsyncCallback, ByVal asyncState As Object) _
As IAsyncResult
End方法的參數(shù)表由一個(gè)IAsyncResult類型的out和by引用參數(shù)組成。
返回值與一個(gè)同步的XML Web服務(wù)方法的返回值類型相同。
By引用參數(shù)是作為輸出參數(shù)列出的。
下面的代碼示例是一個(gè)End方法,返回一個(gè)AuthorRoyalties用戶定義的模式。
[C#]
[WebMethod]
public AuthorRoyalties EndGetAuthorRoyalties(IAsyncResult
asyncResult)
[Visual Basic]
<WebMethod()> _
Public Function EndGetAuthorRoyalties(ByVal asyncResult As _
IAsyncResult) As AuthorRoyalties
下面的代碼示例是一個(gè)和另一個(gè)XML Web服務(wù)方法異步通訊的異步XML Web服務(wù)方法。
[C#]
using System;
using System.Web.Services;
[WebService(Namespace="http://www.contoso.com/")]
public class MyService : WebService {
public RemoteService remoteService;
public MyService() {
// Create a new instance of proxy class for
// the XML Web Service to be called.
remoteService = new RemoteService();
}
// Define the Begin method.
[WebMethod]
public IAsyncResult BeginGetAuthorRoyalties(String Author,AsyncCallback callback, object asyncState) {
// Begin asynchronous communictation with a different XML Web
// service.
return remoteService.BeginReturnedStronglyTypedDS(Author,
callback,asyncState);
}
// Define the End method.
[WebMethod]
public AuthorRoyalties EndGetAuthorRoyalties(IAsyncResultasyncResult) {
// Return the asynchronous result from the other XML Web service.
return remoteService.EndReturnedStronglyTypedDS(asyncResult);
}
}
[Visual Basic]
Imports System.Web.Services
<WebService(Namespace:="http://www.contoso.com/")> _
Public Class MyService
Inherits WebService
Public remoteService As RemoteService
Public Sub New()
MyBase.New()
' Create a new instance of proxy class for
' the XML Web service to be called.
remoteService = New RemoteService()
End Sub
' Define the Begin method.
<WebMethod()> _
Public Function BeginGetAuthorRoyalties(ByVal Author As String, _
ByVal callback As AsyncCallback, ByVal asyncState As Object) _
As IAsyncResult
' Begin asynchronous communictation with a different XML Web
' service.
Return remoteService.BeginReturnedStronglyTypedDS(Author, _
callback, asyncState)
End Function
' Define the End method.
<WebMethod()> _
Public Function EndGetAuthorRoyalties(ByVal asyncResult As _
IAsyncResult) As AuthorRoyalties
' Return the asynchronous result from the other XML Web service.
Return remoteService.EndReturnedStronglyTypedDS(asyncResult)
End Function
End Class