工具软件   办公软件   操作系统   网络安全   设计在线   程序开发   教程宝典   软件下载   软件论坛
您的位置:软件 > 开发者网络 > 微软开发专栏 > Visual Studio.net专栏 > ASP.net > 正文
ASP.NET创建Web服务之异步Web服务
[文章信息]
作者:WAYNE编译
时间:2004-11-18
出处:天极网
责任编辑:方舟
[文章导读]
为了改善调用阻碍线程的长期运行的方法的XML Web服务方法的性能,你应该考虑把它们作为异步的XML Web服务方法发布
advertisement
热点推荐
· ImageReady制作“焰火”小动画
· Java加密和数字签名编程快速入门
· 在VB6中用命令行为模式控制GUI动作
· Excel图表向导详解
· 浅析各种计算机病毒应对方法
[正文]

1 2  下一页

  为了改善调用阻碍线程的长期运行的方法的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


1 2  下一页

天极社区邀请您:写博客日记  上传相片   论坛聊天  订阅电子杂志  推荐网摘   免费图铃工具
笔名:   请您注意:

 遵守国家有关法律、法规,尊重网上道德,承担一切因您的行为而直接或间接引起的法律责任。

 天极网拥有管理笔名和留言的一切权利。
评论:
 
发表评论推荐给朋友我想参加相关培训打印我对此感兴趣订阅电子杂志
相关内容焦点新闻
  • 用ASP.NET结合XML制作广告管理程序
  • ASP.NET 2.0中的登陆控件简介
  • ASP.NET 2.0中的DataSource系列控件
  • 在ASP.NET程序中创建唯一序号
  • 利用ASP.NET 2.0创建自定义Web控件
  • 中兴携手阿尔卡特 全球逐鹿CDMA
  • 用友总裁王文京:誓将ERP变成“大众消费”
  • 香港消费者委员会:数码相机最贵未必最好
  • 外电称中兴正评估西门子手机业务 或能并购
  • 国信办督战八大行业灾难备份 将出台国家标准
  • 中国IT企业走进欧盟 搭第六框架计划直通车
  • 美邦银行用户注意 警惕新型“钓鱼”邮件
  • 微软三年时间磨出新IE 具有更强大安全性能
  • Advertisement

    天极无线


    奇妙科幻|美好风光|清风车影|漫画卡通|星座生肖|明星写真|动物世界
    老鼠爱大米
    挥着翅膀的女孩
    女人味
    栀子花开
    白月光
    刚刚好
    江南
    快乐崇拜
    亲爱的你怎么不在我身边
    小薇
    2002年的第一场雪
    有多少爱可以重来
    我的地盘
    七里香
    情人
     
    老鼠爱大米 老板电话
    冲动的惩罚 七里香
    我不是黄蓉 女生撒娇
    盛夏的果实 坚持到底
    孤单北半球 眉飞色舞
    挪威的森林 可爱女人
    最浪漫的事 老板电话

    CSEEK搜索