為了改善調(diào)用阻礙線程的長期運行的方法的XML Web服務(wù)方法的性能,你應(yīng)該考慮把它們作為異步的XML Web服務(wù)方法發(fā)布。實現(xiàn)一個異步XML Web服務(wù)方法允許線程在返回線程池的時候執(zhí)行其他的代碼。這允許增加一個線程池中的有限數(shù)目的線程,這樣提高了整體性能和系統(tǒng)的可伸縮性。
通常,調(diào)用執(zhí)行輸入/輸出操作的方法的XML Web服務(wù)方法適于作為異步實現(xiàn)。這樣的方法的例子包括和其他的XML Web服務(wù)通訊、訪問遠程數(shù)據(jù)庫、執(zhí)行網(wǎng)絡(luò)輸入/輸出和讀寫大文件方法。這些方法都花費大量時間執(zhí)行硬件級操作,而把線程留著用來執(zhí)行XML Web服務(wù)方法程序塊。如果XML Web服務(wù)方法異步實現(xiàn),那么線程可以被釋放來執(zhí)行其他的代碼。
不管一個XML Web服務(wù)方法是否異步實現(xiàn),客戶端都可以與之異步通訊。使用Web服務(wù)描述語言工具(WSDL.EXE)生成的.net客戶端中的代理類來實現(xiàn)異步通信,即使XML Web服務(wù)方法是同步實現(xiàn)。代理類包含用于與每個XML Web服務(wù)方法異步通信的Begin和End方法。因此,決定一個XML Web服務(wù)方法到底是異步還是同步要取決于性能。
注意:實現(xiàn)一個異步的XML Web服務(wù)方法對客戶端和服務(wù)器上的XML Web服務(wù)之間的HTTP連接沒有影響。HTTP連接既不不會關(guān)閉也不用連接池化。
實現(xiàn)一個異步的XML Web服務(wù)方法
實現(xiàn)一個異步的XML Web服務(wù)方法遵循NET Framework異步設(shè)計模式
把一個同步的XML Web服務(wù)方法分解為兩個方法;其中每個都帶有相同的基名--一個帶有以Begin開頭的名稱,另一個帶有以End開頭的名稱。
Begin方法的參數(shù)表包含方法的功能中的所有的in和by引用參數(shù)。
By引用參數(shù)是作為輸入?yún)?shù)列出的。
倒數(shù)第二個參數(shù)必須是AsyncCallback。AsyncCallback參數(shù)允許客戶端提供一個委托,在方法調(diào)用完成的時候調(diào)用。當(dāng)一個異步XML Web服務(wù)方法調(diào)用另一個異步方法,這個參數(shù)可以被傳入那個方法的倒數(shù)第二個參數(shù)。最后一個參數(shù)是一個對象。對象參數(shù)允許一個調(diào)用者提供狀態(tài)信息給方法。當(dāng)一個異步XML Web服務(wù)方法調(diào)用另一個異步方法,這個參數(shù)可以被傳入那個方法的最后一個參數(shù)。
返回值必須是IAsyncResult類型的。
下面的代碼示例是一個Begin方法,有一個方法函數(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ù)表由一個IAsyncResult類型的out和by引用參數(shù)組成。
返回值與一個同步的XML Web服務(wù)方法的返回值類型相同。
By引用參數(shù)是作為輸出參數(shù)列出的。
下面的代碼示例是一個End方法,返回一個AuthorRoyalties用戶定義的模式。
[C#]
[WebMethod]
public AuthorRoyalties EndGetAuthorRoyalties(IAsyncResult
asyncResult)
[Visual Basic]
<WebMethod()> _
Public Function EndGetAuthorRoyalties(ByVal asyncResult As _
IAsyncResult) As AuthorRoyalties
下面的代碼示例是一個和另一個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ù)方法適于作為異步實現(xiàn)。這樣的方法的例子包括和其他的XML Web服務(wù)通訊、訪問遠程數(shù)據(jù)庫、執(zhí)行網(wǎng)絡(luò)輸入/輸出和讀寫大文件方法。這些方法都花費大量時間執(zhí)行硬件級操作,而把線程留著用來執(zhí)行XML Web服務(wù)方法程序塊。如果XML Web服務(wù)方法異步實現(xiàn),那么線程可以被釋放來執(zhí)行其他的代碼。
不管一個XML Web服務(wù)方法是否異步實現(xiàn),客戶端都可以與之異步通訊。使用Web服務(wù)描述語言工具(WSDL.EXE)生成的.net客戶端中的代理類來實現(xiàn)異步通信,即使XML Web服務(wù)方法是同步實現(xiàn)。代理類包含用于與每個XML Web服務(wù)方法異步通信的Begin和End方法。因此,決定一個XML Web服務(wù)方法到底是異步還是同步要取決于性能。
注意:實現(xiàn)一個異步的XML Web服務(wù)方法對客戶端和服務(wù)器上的XML Web服務(wù)之間的HTTP連接沒有影響。HTTP連接既不不會關(guān)閉也不用連接池化。
實現(xiàn)一個異步的XML Web服務(wù)方法
實現(xiàn)一個異步的XML Web服務(wù)方法遵循NET Framework異步設(shè)計模式
把一個同步的XML Web服務(wù)方法分解為兩個方法;其中每個都帶有相同的基名--一個帶有以Begin開頭的名稱,另一個帶有以End開頭的名稱。
Begin方法的參數(shù)表包含方法的功能中的所有的in和by引用參數(shù)。
By引用參數(shù)是作為輸入?yún)?shù)列出的。
倒數(shù)第二個參數(shù)必須是AsyncCallback。AsyncCallback參數(shù)允許客戶端提供一個委托,在方法調(diào)用完成的時候調(diào)用。當(dāng)一個異步XML Web服務(wù)方法調(diào)用另一個異步方法,這個參數(shù)可以被傳入那個方法的倒數(shù)第二個參數(shù)。最后一個參數(shù)是一個對象。對象參數(shù)允許一個調(diào)用者提供狀態(tài)信息給方法。當(dāng)一個異步XML Web服務(wù)方法調(diào)用另一個異步方法,這個參數(shù)可以被傳入那個方法的最后一個參數(shù)。
返回值必須是IAsyncResult類型的。
下面的代碼示例是一個Begin方法,有一個方法函數(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ù)表由一個IAsyncResult類型的out和by引用參數(shù)組成。
返回值與一個同步的XML Web服務(wù)方法的返回值類型相同。
By引用參數(shù)是作為輸出參數(shù)列出的。
下面的代碼示例是一個End方法,返回一個AuthorRoyalties用戶定義的模式。
[C#]
[WebMethod]
public AuthorRoyalties EndGetAuthorRoyalties(IAsyncResult
asyncResult)
[Visual Basic]
<WebMethod()> _
Public Function EndGetAuthorRoyalties(ByVal asyncResult As _
IAsyncResult) As AuthorRoyalties
下面的代碼示例是一個和另一個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