|
[C#] 源代码网推荐using System.Web.Services; 源代码网推荐using System.Data; 源代码网推荐using System; 源代码网推荐// This imports the proxy class for the XML Web services 源代码网推荐// that the sample communicates with. 源代码网推荐using AsyncWS.localhost; 源代码网推荐 源代码网推荐namespace AsyncWS 源代码网推荐{ 源代码网推荐 [WebService(Namespace="http://www.contoso.com/")] 源代码网推荐 public class MyService : System.Web.Services.WebService 源代码网推荐 { 源代码网推荐 public RemoteService remoteService; 源代码网推荐 public MyService() 源代码网推荐 { 源代码网推荐 remo teService = new RemoteService(); 源代码网推荐 } 源代码网推荐 源代码网推荐 [WebMethod] 源代码网推荐 public IAsyncResult BeginGetAuthorRoyalties(String Author, 源代码网推荐 AsyncCallback callback, Object asyncState) 源代码网推荐 { 源代码网推荐 // Saves the current state for the call that gets the author"s 源代码网推荐 // royalties. 源代码网推荐 AsyncStateChain state = new AsyncStateChain(); 源代码网推荐 state.originalState = asyncState; 源代码网推荐 state.Author = Author; 源代码网推荐 state.originalCallback = callback; 源代码网推荐 源代码网推荐 // Creates an intermediary callback. 源代码网推荐 AsyncCallback chainedCallback = new 源代码网推荐 AsyncCallback(AuthorRoyaltiesCallback); 源代码网推荐 return remoteService.BeginGetAuthors(chainedCallback,state); 源代码网推荐 } 源代码网推荐 // Intermediate method to handle chaining the 源代码网推荐 // asynchronous calls. 源代码网推荐 public void AuthorRoyaltiesCallback(IAsyncResult ar) 源代码网推荐 { 源代码网推荐 AsyncStateChain state = (AsyncStateChain)ar.AsyncState; 源代码网推荐 RemoteService rs = new RemoteService(); 源代码网推荐 源代码网推荐 // Gets the result from the call to GetAuthors. 源代码网推荐 Authors allAuthors = rs.EndGetAuthors(ar); 源代码网推荐 源代码网推荐 Boolean found = false; 源代码网推荐 // Verifies that the requested author is valid. 源代码网推荐 int i = 0; 源代码网推荐 DataRow row; 源代码网推荐 while (i < allAuthors.authors.Rows.Count && !found) 源代码网推荐 { 源代码网推荐 row = allAuthors.authors.Rows[i]; 源代码网推荐 if (row["au_lname"].ToString() == state.Author) 源代码网推荐 { 源代码网推荐 found = true; 源代码网推荐 } 源代码网推荐 i++; 源代码网推荐 } 源代码网推荐 if (found) 源代码网推荐 { 源代码网推荐 AsyncCallback cb = state.originalCallback; 源代码网推荐 // Calls the second XML Web service, because the author is 源代码网推荐 // valid. 源代码网推荐 rs.BeginReturnedStronglyTypedDS(state.Author,cb,state); 源代码网推荐 } 源代码网推荐 else 源代码网推荐 { 源代码网推荐 // Cannot throw the exception in this function or the XML Web 源代码网推荐 // service will hang. So, set the state argument to the 源代码网推荐 // exception and let the End method of the chained XML Web 源代码网推荐 // service check for it. 源代码网推荐 ArgumentException ex = new ArgumentException( 源代码网推荐 "Author does not exist.","Author"); 源代码网推荐 AsyncCallback cb = state.originalCallback; 源代码网推荐 // Call the second XML Web service, setting the state to an 源代码网推荐 // exception. 源代码网推荐 rs.BeginReturnedStronglyTypedDS(state.Author,cb,ex); 源代码网推荐 } 源代码网推荐 } 源代码网推荐 源代码网推荐 [WebMethod] 源代码网推荐 public AuthorRoyalties EndGetAuthorRoyalties(IAsyncResult asyncResult) 源代码网推荐 { 源代码网推荐 // Check whehter the first XML Web service threw an exception. 源代码网推荐 if (asyncResult.AsyncState is ArgumentException) 源代码网推荐 throw (ArgumentException) asyncResult.AsyncState; 源代码网推荐 else 源代码网推荐 return remoteService.EndReturnedStronglyTypedDS(asyncResult); 源代码网推荐 } 源代码网推荐} 源代码网推荐// Class to wrap the callback and state for the intermediate 源代码网推荐// asynchronous operation. 源代码网推荐public class AsyncStateChain 源代码网推荐{ 源代码网推荐 public AsyncCallback originalCallback; 源代码网推荐 public Object originalState; 源代码网推荐 public String Author; 源代码网推荐} 源代码网推荐} 源代码网推荐 源代码网推荐[Visual Basic] 源代码网推荐 源代码网推荐Imports System.Web.Services 源代码网推荐Imports System.Data 源代码网推荐Imports System 源代码网推荐" This imports the proxy class for the XML Web services 源代码网推荐" that the sample communicates with. 源代码网推荐Imports AsyncWS_VB.localhost 源代码网推荐 源代码网推荐Namespace AsyncWs 源代码网推荐 源代码网推荐<WebService(Namespace:="http://www.contoso.com/")> _ 源代码网推荐Public Class MyService 源代码网推荐Inherits WebService 源代码网推荐Public remoteService As remoteService 源代码网推荐Public Sub New() 源代码网推荐MyBase.New() 源代码网推荐remoteService = New localhost.RemoteService() 源代码网推荐End Sub 源代码网推荐" Defines the Begin method. 源代码网推荐<WebMethod()> _ 源代码网推荐Public Function BeginGetAuthorRoyalties(ByVal Author As String, _ 源代码网推荐ByVal callback As AsyncCallback, ByVal asyncState As Object) _ 源代码网推荐As IAsyncResult 源代码网推荐" Saves the current state for the call that gets the author"s 源代码网推荐" royalties. 源代码网推荐Dim state As AsyncStateChain = New AsyncStateChain() 源代码网推荐state.originalState = asyncState 源代码网推荐state.Author = Author 源代码网推荐state.originalCallback = callback 源代码网推荐 源代码网推荐" Creates an intermediary callback. 源代码网推荐Dim chainedCallback As AsyncCallback = New AsyncCallback( _ 源代码网推荐AddressOf AuthorRoyaltiesCallback) 源代码网推荐" Begin asynchronous communictation with a different XML Web 源代码网推荐" service. 源代码网推荐Return remoteService.BeginGetAuthors(chainedCallback, state) 源代码网推荐End Function 源代码网推荐 源代码网推荐" Intermediate method to handle chaining the asynchronous calls. 源代码网推荐Public Sub AuthorRoyaltiesCallback(ByVal ar As IAsyncResult) 源代码网推荐Dim state As AsyncStateChain = CType(ar.AsyncState, _ 源代码网推荐AsyncStateChain) 源代码网推荐Dim rs As RemoteService = New RemoteService() 源代码网推荐 源代码网推荐" Gets the result from the call to GetAuthors. 源代码网推荐Dim allAuthors As Authors = rs.EndGetAuthors(ar) 源代码网推荐Dim found As Boolean = False 源代码网推荐 源代码网推荐" Verifies that the requested author is valid. 源代码网推荐Dim i As Integer = 0 源代码网推荐Dim row As DataRow 源代码网推荐While (i < allAuthors.authors.Rows.Count And (Not found)) 源代码网推荐 row = allAuthors.authors.Rows(i) 源代码网推荐 If (row("au_lname").ToString() = state.Author) Then 源代码网推荐 found = True 源代码网推荐 End If 源代码网推荐 i = i + 1 源代码网推荐End While 源代码网推荐 源代码网推荐If (found) Then 源代码网推荐 Dim cb As AsyncCallback = state.originalCallback 源代码网推荐 " Calls the second XML Web service, because the author is 源代码网推荐 " valid. 源代码网推荐 rs.BeginReturnedStronglyTypedDS(state.Author, cb, state) 源代码网推荐Else 源代码网推荐 " Cannot throw the exception in this function or the XML Web 源代码网推荐 " service will hang. So, set the state argument to the 源代码网推荐 " exception and let the End method of the chained XML Web 源代码网推荐 " service check for it. 源代码网推荐 Dim ex As ArgumentException = New ArgumentException( "Author does not exist.", "Author") 源代码网推荐 Dim cb As AsyncCallback = state.originalCallback 源代码网推荐 " Call the second XML Web service, setting the state to an 源代码网推荐 " exception. 源代码网推荐 rs.BeginReturnedStronglyTypedDS(state.Author, cb, ex) 源代码网推荐End If 源代码网推荐End Sub 源代码网推荐 源代码网推荐" Define the End method. 源代码网推荐<WebMethod()> _ 源代码网推荐Public Function EndGetAuthorRoyalties(ByVal asyncResult As _ 源代码网推荐IAsyncResult) As localhost.AuthorRoyalties 源代码网推荐 " Return the asynchronous result from the other XML Web service. 源代码网推荐 Return remoteService.EndReturnedStronglyTypedDS(asyncResult) 源代码网推荐End Function 源代码网推荐 源代码网推荐End Class 源代码网推荐 源代码网推荐" Class to wrap the callback and state for the intermediate asynchronous 源代码网推荐" operation. 源代码网推荐Public Class AsyncStateChain 源代码网推荐Public originalCallback As AsyncCallback 源代码网推荐Public originalState As Object 源代码网推荐Public Author As String 源代码网推荐End Class 源代码网推荐End Namespace |