在Atlas服务器端实现中推荐使用Web Service而不是Page Method

English Version: http://dflying.dflying.net/1/archive/107_prefer_web_services_to_page_methods_in_atlas_server_side_implementation.html

我们可以用两种方式把一个服务器段方法暴露给客户端Atlas调用:Web ServicePage Method。我推荐使用Web Service的方法。

所有人都应该非常重视的一点是Web ServicePage Method的工作原理以及工作过程有很大的分别。对于Atlas调用Web Service来说,当请求被发送时候,仅仅简单传给服务器方法的参数数据。而对于Atlas调用Page Method来说,传输的数据将会很多,将把表单中所有的域,包括ViewState,一起传送到服务器。在服务器端,它的工作方式也和普通的PostBack很相似:在这个Page Method被调用前,所有的服务器控件将得到它自身的状态。这也正是为什么Page Method中可以访问页面中控件状态的原因。

因此我的建议是只要在确实需要使用Page Method的时候(比如说需要在Page Method中访问页面中的控件状态)才使用Page Method,否则尽可能多地使用Web Service,这样可以使程序在性能上有所提高。使用Web Service的另一个好处是让程序层次架构明晰。

您可以在http://www.fiddlertool.com/fiddler/下载Fiddler,一个很好的HTTP watcherdebugger,用来监视客户端与服务器的实际HTTP通信内容。

这里是一段Web Method,只是简单的返回服务器的当前时间。

[WebMethod]
public DateTime GetCurrentDateTime()
{
    
return DateTime.Now;
}

让我们分别以Web ServicePage Method运行这一段Web Method,并用Fiddler观察实际运行时的HTTP通信内容:

Web Service

Page Method

可以看到以Web Service方式运行时Post回服务器的Content-Length0,而以Page Method运行时候为1718

posted on 2006-04-03 18:15  Dflying Chen  阅读(5659)  评论(48编辑  收藏  举报