当前位置:首页 > 网络编程 > WEB编程 > ASP.net > ASP.NET创建XML Web服务全接触(9)

ASP.NET创建XML Web服务全接触(9)

点击次数:20 次 发布日期:2008-11-26 23:06:43 作者:源代码网
源代码网推荐
异步Web服务(1)

源代码网推荐    

  为了改善调用阻碍线程的长期运行的方法的XML Web服务方法的性能,你应该考虑把它们作为异步的XML Web服务方法发布。实现一个异步XML Web服务方法允许线程在返回线程池的时候执行其他的代码。这允许增加一个线程池中的有限数目的线程,这样提高了整体性能和系统的可伸缩性。
源代码网推荐
源代码网推荐  通常,调用执行输入/输出操作的方法的XML Web服务方法适于作为异步实现。这样的方法的例子包括和其他的XML Web服务通讯、访问远程数据库、执行网络输入/输出和读写大文件方法。这些方法都花费大量时间执行硬件级操作,而把线程留着用来执行XML Web服务方法程序块。如果XML Web服务方法异步实现,那么线程可以被释放来执行其他的代码。
源代码网推荐
源代码网推荐  不管一个XML Web服务方法是否异步实现,客户端都可以与之异步通讯。使用Web服务描述语言工具(WSDL.EXE)生成的.NET客户端中的代理类来实现异步通信,即使XML Web服务方法是同步实现。代理类包含用于与每个XML Web服务方法异步通信的Begin和End方法。因此,决定一个XML Web服务方法到底是异步还是同步要取决于性能。
源代码网推荐
源代码网推荐  注意:实现一个异步的XML Web服务方法对客户端和服务器上的XML Web服务之间的HTTP连接没有影响。HTTP连接既不不会关闭也不用连接池化。
源代码网推荐
源代码网推荐  实现一个异步的XML Web服务方法
源代码网推荐
源代码网推荐  实现一个异步的XML Web服务方法遵循NET Framework异步设计模式
源代码网推荐
源代码网推荐  把一个同步的XML Web服务方法分解为两个方法;其中每个都带有相同的基名--一个带有以Begin开头的名称,另一个带有以End开头的名称。
源代码网推荐
源代码网推荐  Begin方法的参数表包含方法的功能中的所有的in和by引用参数。
源代码网推荐
源代码网推荐  By引用参数是作为输入参数列出的。
源代码网推荐
源代码网推荐  倒数第二个参数必须是AsyncCallback。AsyncCallback参数允许客户端提供一个委托,在方法调用完成的时候调用。当一个异步XML Web服务方法调用另一个异步方法,这个参数可以被传入那个方法的倒数第二个参数。最后一个参数是一个对象。对象参数允许一个调用者提供状态信息给方法。当一个异步XML Web服务方法调用另一个异步方法,这个参数可以被传入那个方法的最后一个参数。
源代码网推荐
源代码网推荐  返回值必须是IAsyncResult类型的。
源代码网推荐
源代码网推荐  下面的代码示例是一个Begin方法,有一个方法函数特定的String参数。
源代码网推荐
源代码网推荐

[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方法的参数表由一个IAsyncResult类型的out和by引用参数组成。
源代码网推荐
源代码网推荐  返回值与一个同步的XML Web服务方法的返回值类型相同。
源代码网推荐
源代码网推荐  By引用参数是作为输出参数列出的。
源代码网推荐
源代码网推荐  下面的代码示例是一个End方法,返回一个AuthorRoyalties用户定义的模式。
源代码网推荐
源代码网推荐

[C#]
源代码网推荐[WebMethod]
源代码网推荐public AuthorRoyalties EndGetAuthorRoyalties(IAsyncResult
源代码网推荐asyncResult)
源代码网推荐
源代码网推荐[Visual Basic]
源代码网推荐<WebMethod()> _
源代码网推荐Public Function EndGetAuthorRoyalties(ByVal asyncResult As _
源代码网推荐IAsyncResult) As AuthorRoyalties


源代码网推荐  下面的代码示例是一个和另一个XML Web服务方法异步通讯的异步XML Web服务方法。
源代码网推荐
源代码网推荐

[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


源代码网推荐  下面的代码示例显示当一个XML Web服务方法产生了一个以上的异步调用并且这些调用必须连续执行时如何连接这些异步调用。BeginGetAuthorRoyalties方法产生一个异步调用用来判断传入的作者名是否有效,并设置一个名为AuthorRoyaltiesCallback的中间回调来接收结果。如果作者名有效,那么那个中间回调异步调用来获得作者的版税。
源代码网推荐
源代码网推荐

[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


源代码网推荐
源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华