您的位置:首页 > 编程语言

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)

2008-07-21 08:30 357 查看
[索引页]

[源码下载]

[align=center]化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)[/align]

作者:webabcd

介绍

WCF(Windows Communication Foundation) - Web编程模型:使用WCF创建REST服务,使用asp.net ajax调用WCF服务

·System.ServiceModel.Activation.WebServiceHostFactory - 用于承载使用 WCF Web 编程模型的服务

·System.ServiceModel.Activation.WebScriptServiceHostFactory - 能够向服务中自动添加 ASP.NET AJAX 终结点而无需进行配置

示例(使用WCF创建REST服务)

1、服务

User.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.ServiceModel;

using System.Runtime.Serialization;

namespace WCF.ServiceLib.Web

IREST.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.ServiceModel;

using System.ServiceModel.Web;

namespace WCF.ServiceLib.Web

REST.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.ServiceModel;

namespace WCF.ServiceLib.Web

2、宿主

REST.svc

<?xml version="1.0"?>

<configuration>

<system.serviceModel>

<behaviors>

<serviceBehaviors>

<behavior name="WebBehavior">

<!--httpGetEnabled - 指示是否发布服务元数据以便使用 HTTP/GET 请求进行检索,如果发布 WSDL,则为 true,否则为 false,默认值为 false-->

<serviceMetadata httpGetEnabled="true" />

</behavior>

</serviceBehaviors>

<endpointBehaviors>

<behavior name="RESTBehavior">

<!--webHttp - 启用 WCF 服务的 Web 编程模型-->

<webHttp />

</behavior>

</endpointBehaviors>

</behaviors>

<services>

<!--name - 提供服务的类名-->

<!--behaviorConfiguration - 指定相关的服务行为配置-->

<service name="WCF.ServiceLib.Web.REST" behaviorConfiguration="WebBehavior">

<!--address - 服务地址-->

<!--binding - 通信方式-->

<!--contract - 服务契约-->

<!--behaviorConfiguration - 指定相关的端点行为配置-->

<endpoint address="" binding="webHttpBinding" contract="WCF.ServiceLib.Web.IREST" behaviorConfiguration="RESTBehavior" />

</service>

</services>

</system.serviceModel>

</configuration>

3、客户端

REST.aspx

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">

</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">

<asp:Label ID="lblMsg" runat="server" />

</asp:Content>

REST.aspx.cs

using System;

using System.Collections;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

using System.Net;

public partial class Web_REST : System.Web.UI.Page

运行结果:

{"Name":"webabcd","DayOfbirth":"\/Date(319305600000+0800)\/"}

{"Name":"webabcd","DayOfbirth":"\/Date(319305600000+0800)\/"}

true

true

示例(使用asp.net ajax调用WCF服务)

1、服务

User.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.ServiceModel;

using System.Runtime.Serialization;

namespace WCF.ServiceLib.Web

IAJAX.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.ServiceModel;

namespace WCF.ServiceLib.Web

AJAX.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.ServiceModel;

using System.ServiceModel.Activation;

namespace WCF.ServiceLib.Web

2、宿主

AJAX.svc

<?xml version="1.0"?>

<configuration>

<system.serviceModel>

<behaviors>

<serviceBehaviors>

<behavior name="WebBehavior">

<!--httpGetEnabled - 指示是否发布服务元数据以便使用 HTTP/GET 请求进行检索,如果发布 WSDL,则为 true,否则为 false,默认值为 false-->

<serviceMetadata httpGetEnabled="true" />

</behavior>

</serviceBehaviors>

<endpointBehaviors>

<behavior name="AJAXBehavior">

<!--enableWebScript - 启用 WCF 服务的 脚本 编程模型-->

<enableWebScript />

</behavior>

</endpointBehaviors>

</behaviors>

<services>

<!--name - 提供服务的类名-->

<!--behaviorConfiguration - 指定相关的服务行为配置-->

<service name="WCF.ServiceLib.Web.AJAX" behaviorConfiguration="WebBehavior">

<!--address - 服务地址-->

<!--binding - 通信方式-->

<!--contract - 服务契约-->

<!--behaviorConfiguration - 指定相关的端点行为配置-->

<endpoint address="" binding="webHttpBinding" contract="WCF.ServiceLib.Web.IAJAX" behaviorConfiguration="AJAXBehavior" />

</service>

</services>

</system.serviceModel>

</configuration>

3、客户端

Demo.aspx

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

<title>AJAX调用WCF</title>

</head>

<body>

<form id="form1" runat="server">

<asp:ScriptManager ID="ScriptManager1" runat="server">

<Services>

<asp:ServiceReference Path="Service/AJAX.svc" />

</Services>

</asp:ScriptManager>

<asp:Label ID="lblMsg" runat="server" />

</form>

</body>

</html>

运行结果:

姓名:webabcd

生日:1980-02-14

OK

[源码下载]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: