WCF 第十三章 可编程站点 使用AJAX和JSON进行网页编程

来源:互联网 发布:博客论坛群发软件 编辑:程序博客网 时间:2024/06/05 02:34

======================================================
注:本文源代码点此下载
======================================================

到目前为止我们已经看了如何使用webhttpbinding绑定和webhttpbehavior终结点行为来寄宿服务。这允许我们使用pox来暴露服务。很多网站开发人员想放弃使用xml而使用json,一个更简单的格式。json非常适合需要一个高效地从服务截取反馈消息的浏览器应用程序,它已经集成了javascript的优势,编程语言必须通常使用客户端网络开发。json是javascript的对象符号元素子集,这意味着你可以很容易地在javascript中创建对象。由于这个原因,xml与ajax应用程序一起使用是很好的。

ajax表示异步javascript和xml。基于ajax的网络应用程序比传统的应用程序有重要的优势。它们允许改进的用户体验以及更好的带宽可供使用。这通过改进浏览器到服务器通信实现,以便于浏览器不需要执行一个页面载入。这通过使用javascript和xmlhttprequest类与一台服务器异步通信。由于与服务器通信不需要载入界面就能完成,开发人员可以创建接近桌面应用程序的富用户接口体验。这些网络应用程序的类型通常是指富互联网应用程序,或者rias.

asp.net ajax 集成

有很多创建这些基于ajax的网络应用程序平台。其中一个有名的平台是asp.net ajax 平台。这个平台有一个好的创建开启ajax网络应用程序的客户端和服务端模型。它包含很多能力比如一个富客户端类库,富ajax网页控件,为与服务通信而自动生成的客户端代理。它也基于asp.net,使用微软技术来使用.net创建网络应用程序。wcf在.net framework 3.0 时也与asp.net 集成。.net framework 3.5 使用webscriptenablingbehavior终结点行为引入对asp.net ajax 应用的支持。这取代了webhttpbehavior终结点行为。它默认使用json和asp.net 客户端代理生成添加支持。这些新的特性可以通过使用enablewebscript配置元素来取代webhttp终结点行为配置元素使用。

我们创建一个称为xbox 360 游戏审查程序来看我们如何使用webhttpbinding绑定以及webscriptenablingbehavior来创建基于asp.net ajax 的示例应用程序。这个简单的应用程序允许用户对他们喜爱的xbox 360 游戏提供审查。这个应用使用visual studio 2008 中asp.net ajax 网站模板创建。

图片13.2 显示了这个站点的图片。

图片13.2 xbox 360 ajax 开启应用的游戏审查视图

这个站点有很多特性。首先是在一个listbox控件中显示给用户的游戏列表。用户可以选择一个游戏并查看每个游戏的评论。然后一个用户可以为每个游戏添加评论。列表13.7 列出了提供这个功能的服务。

列表13.7 gamereviewservice.cs

using system;

using system.collections.generic;

using system.runtime.serialization;

using system.servicemodel;

using system.text;

using system.servicemodel.activation;

using system.servicemodel.web;

namespace essentialwcf

{

[servicecontract(namespace = "essentialwcf")]

[servicebehavior(instancecontextmode = instancecontextmode.single)]

[aspnetcompatibilityrequirements(requirementsmode = aspnetcompatibilityrequirementsmode.allowed)]

public class gamereviewservice

{

private string[] gamelist = new string[] { "viva pinata", "star wars lego",

"spiderman ultimate","gears of war", "halo 2", "halo 3"};

private dictionary> reviews;

public gamereviewservice()

{

reviews = new dictionary>();

foreach (string game in gamelist)

{

reviews.add(game, new list());

}

}

[operationcontract]

[webget]

public string[] games()

{

return gamelist;

}

[operationcontract]

[webget]

public string[] reviews(string game)

{

weboperationcontext ctx = weboperationcontext.current;

ctx.outgoingresponse.headers.add("cache-control", "no-cache");

if (!reviews.containskey(game))

{

return null;

}

list listofreviews = reviews[game];

if (listofreviews.count == 0)

{

return new string[] { string.format("no reviews found for {0}.", game) };

}

else

{

return listofreviews.toarray();

}

}

[operationcontract]

[webinvoke]

public void addreview(string game, string comment)

{

reviews[game].add(comment);

}

[operationcontract]

[webinvoke]

public void clearreviews(string game)

{

reviews[game].clear();

}

}

}

我们选择在因特网信息服务(iis)中寄宿这个服务。列表13.8 显示了用来寄宿服务的gamereviewservice.svc。

列表13.8 gamereviewservice.svc

列表13.9 显示了用户寄宿gamereviewservice的配置信息。配置信息最重要的方面是使用webhttpbinding绑定和enablewebscript终结点行为。这开启了使用json并使用asp.net 为gamereviewservice生成必要的客户端代理代码。

列表13.9 web.config

你可以通过使用asp.net scriptmanager向服务添加一个引用来配置gamereviewservice 被asp.net 使用。列表13.10 显示了标记用来引用gamereviewservice。在这个场景后面生成了一个引用一个javascript文件和客户端代理的客户端脚本。对我们的例子来说,客户端javascript的uri是http://localhost/gamereviewservice/gamereviewservice.svc/js.

列表13.10 使用asp.net scriptmanager引用服务

我们已经包含了用来创建xbox 360 游戏审查网络应用程序的asp.net web 窗体。这显示了服务如何从客户端脚本被调用以及结果如何被动态地显示到控制界面上。

列表13.11 进行客户端代理调用

xbox 360 game reviews

xbox 360 game review

style="width:240px">select a game:

commnets:

enter a comment:


======================================================
在最后,我邀请大家参加新浪APP,就是新浪免费送大家的一个空间,支持PHP+MySql,免费二级域名,免费域名绑定 这个是我邀请的地址,您通过这个链接注册即为我的好友,并获赠云豆500个,价值5元哦!短网址是http://t.cn/SXOiLh我创建的小站每天访客已经达到2000+了,每天挂广告赚50+元哦,呵呵,饭钱不愁了,\(^o^)/