选择显示字体大小

asp.net2.0中数据源控件之异步数据访问


  在第 1 部分和第 2 部分中,建立了 weatherdatasource 控件,该控件针对 weather.com(英文)所提供的 xml api 来运行,使用 webrequest 和 webresponse 来通过 http 访问数据。迄今为止,均是同步访问该服务。因此,页面处理被阻止,直到 web 请求完成为止。 此方法对于测试页面是有效的,在小站点上也可能有效,但是在接收大量通信流量的站点上则会惨败;例如门户页面,天气模块在其中可能非常常见。

  引言

  在线程池中有固定不变的大量线程可用于服务请求,遗憾的是,该解决方案并非仅仅提高限制(还会增加线程占用资源以及 cpu 占用资源)。因此,当一个页面被阻止而等候另一个服务器时,它还在占用线程,因而可能会导致其他传入的请求在队列中等候更长的时间。这将导致对站点的访问变慢,并降低 cpu 的利用率。在 visual studio 2005 中,我们引入了异步页面,这使得控件能够定义它们希望异步完成的任务,即,无需阻止用来处理请求的线程。在此将不介绍异步页面本身的详细信息,dmitry(英文)和 fritz onion(英文)中以前已经有所介绍。此处要介绍的是如何在数据源控件中利用此功能,使用加载项框架来实现异步数据源。

  背景

  在第 1 部分中,间接提到了 datasourceview 类的有些古怪的设计:

public abstract class datasourceview {
 public virtual void select(datasourceselectarguments arguments,
  datasourceviewselectcallback callback);
 protected abstract ienumerable executeselect(
  datasourceselectarguments arguments);
 ...
}

  您会注意到,公共 select 方法实际上并不返回任何数据。而是接受一个回拨,并通过该回拨来返回数据。它只调用受保护的 executeselect(它始终执行同步数据访问)来检索要退还给数据绑定控件的数据。datasourceview 类的默认实现实际上不会异步执行任何操作。原因在于,并不存在任何现成的异步数据源控件。但 om 的设计确实允许实现异步数据访问,在这种设计下,数据在异步工作完成之后才可用。因此,我们就有了一个基于回拨的模型。

  那些熟悉框架中的异步 api 的人会注意到缺少了异步模式:公共 select、beginselect 和 endselect 方法,在这些方法中,数据绑定控件选择要调用哪些方法。但是,数据绑定控件并不能确定是选择同步 api 还是选择异步 api。此外,在数据绑定控件上添加属性也毫无作用。数据源控件封装了有关如何访问数据存储的详细信息,对数据存储的访问是同步发生还是异步发生应该根据数据源是否基于语义来决定或者根据自定义属性来决定。潜在的“bool performasyncdataaccess”属性的正确位置适合于数据源控件本身。这还使得数据源控件可以使用一种方法来执行数据访问,即使多个数据绑定控件被绑定到同一个数据源。至此已多次解释了该体系结构所蕴涵的这些微妙的概念,但愿能阐明该设计。

  关于异步任务,最后要注意的一点是:页面是否应该执行任何异步工作完全由页面开发人员最终决定(通过 page 指令的 async 属性)。因此,任何编写良好的数据源控件必须退化为根据需要来执行同步数据访问。

  框架

  在此框架中(在此系列结尾会用示例的剩余部分来演示这一点),已将 asyncdatasource 和 asyncdatasourceview 基类放在一起,这些基类可以用于实现能够执行异步数据访问的数据源控件。以下大概介绍了框架内容,以及有助于弄清楚其含义的一些注释:

public abstract class asyncdatasourcecontrol : datasourcecontrol,
iasyncdatasource {
private bool _performasyncdataaccess;

protected asyncdatasourcecontrol() {
 _performasyncdataaccess = true;
}

public virtual bool performasyncdataaccess {
 get; set;
}

bool iasyncdatasource.isasync {
 get { return _performasyncdataaccess && page.isasync; }
}
}

public abstract class asyncdatasourceview : datasourceview {

 protected abstract iasyncresult beginexecuteselect(
  datasourceselectarguments arguments,
  asynccallback asynccallback,
  object asyncstate);

 protected abstract ienumerable endexecuteselect(
  iasyncresult asyncresult);

  protected override ienumerable executeselect(
   datasourceselectarguments arguments) {
    //实现从 datasourceview 中继承的
    //抽象 executeselect 方法,
    //方法是使用 beginexecuteselect 和 endexecuteselect,
    //以便通过阻止来
    //进行同步数据访问。
   }

   private iasyncresult onbeginselect(object sender,
     eventargs e, asynccallback asynccallback,
     object extradata);
   private void onendselect(iasyncresult asyncresult);

   public override void select(datasourceselectarguments arguments,
    datasourceviewselectcallback callback) {
     if (_owner.isasync) {
      //使用 onbeginselect 和 onendselect
      //作为 begineventhandler 和 endeventhandler 方法,
      //来调用 page.registerasynctask,
      //以指明需要
      //进行异步工作。这些方法将依次
      //调用特定的
      //数据源实现,方法是调用
      //已在此类中引入的
      //抽象 beginexecuteselect 和 endexecuteselect
      //方法。
     }
     else {
      //执行同步数据访问
      base.select(arguments, callback);
     }
    }
   ...
}

  示例

  现在,新的 asyncweatherdatasource 将从 asyncdatasourcecontrol 中派生,而 asyncweatherdatasourceview 将从 asyncdatasourceview 中派生。

public class asyncweatherdatasource : asyncdatasourcecontrol {

//与 weatherdatasource 相同
}

private sealed class asyncweatherdatasourceview : asyncdatasourceview {
private asyncweatherdatasource _owner;
private weatherservice _weatherservice;

public asyncweatherdatasourceview(asyncweatherdatasource owner,
string viewname)
: base(owner, viewname) {
_owner = owner;
}

protected override iasyncresult beginexecuteselect(datasourceselectarguments arguments,
asynccallback asynccallback,
object asyncstate) {
arguments.raiseunsupportedcapabilitieserror(this);

string zipcode = _owner.getselectedzipcode();
if (zipcode.length == 0) {
return new synchronousasyncselectresult(/* selectresult */
null,
asynccallback, asyncstate);
}

_weatherservice = new weatherservice(zipcode);
return _weatherservice.begingetweather(asynccallback, asyncstate);
}

protected override ienumerable endexecuteselect(iasyncresult asyncresult) {
synchronousasyncselectresult syncresult =
asyncresult as synchronousasyncselectresult;
if (syncresult != null) {
return syncresult.selectresult;
}
else {
weather weatherobject =
_weatherservice.endgetweather(asyncresult);
_weatherservice = null;

if (weatherobject != null) {
return new weather[] { weatherobject };
}
}

return null;
}
}

  要注意的关键问题是,在使用该框架时,只需要实现 beginexecuteselect 和 endexecuteselect。在它们的实现过程中,通常要调用由该框架中的各种对象(例如 webrequest 或 io 流)所揭示的 beginxxx 和 endxxx 方法(在 visual studio 2005 中,还需要调用 sqldatacommand),并返回 iasyncresult。在此示例中,有一个封装了基础 webrequest 对象的 weatherservice 帮助程序类。

  对于那些实际缺少异步模式框架,您在此会看到有效的异步模式;以及您要实现的 beginexecuteselect 和 endexecuteselect,和您要调用以返回 iasyncresult 实例的 begin 和 end 方法。

  最有趣的可能是 synchronousasyncselectresult 类(在某种程度上而言是一种矛盾)。此类是框架附带的。它基本上是一个 iasyncresult 实现,可使数据立即可用,并从其 iasyncresult.completedsynchronously 属性报告 true。到目前为止,这仅适用于未选择邮政编码的情况,并且需要返回 null(启动异步任务而只返回 null 是没有意义的),但正如您会在下文中看到的,这在其他方案中也是有用的。

  页面基础结构隐藏了在 microsoft asp.net 上下文中执行异步工作的大部分详细信息。希望在此提供的框架使您执行最少的操作就能编写使用此基础结构的数据源。不过,就其本质而言,实现异步行为是复杂的。有时候,第一次阅读本文时会有一些疑问,而第二次阅读时可能就明白了。您可以使用下面“我的评论”表单来发送问题或进行讨论。


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons