化零为整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
[源码下载]
[源码下载]
[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
[源码下载]
相关文章推荐
- 化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)
- 化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)
- Web编程模型(WCF创建REST, AJAX调用WCF)
- 简单介绍使用WCF的Web编程模型开发REST风格的Web Service
- 简单介绍使用WCF的Web编程模型开发REST风格的Web Service
- 使用 WCF Web HTTP 编程模型创建返回任意数据的服务
- 使用 WCF REST 编程模型创建接受任意数据的服务
- 如何让WCF服务更好地支持Web Request和AJAX调用
- 如何让WCF服务更好地支持Web Request和AJAX调用
- 如何让WCF服务更好地支持Web Request和AJAX调用
- WCF的Web编程模型资源
- WCF的Web编程模型资源
- 【菜鸟学WCF】使用ScriptManager+Ajax调用WCF服务之如何为服务创建接口
- 关于WCF REST(webHttpBinding ) 用法:服务端写法及客户端调用
- WCF Web编程模型
- WCF的Web编程模型资源
- WCF的Web编程模型资源
- WCF的Web编程模型资源
- 使用C#创建webservice及三种调用方式 (ASP.NETweb编程常用到的27个函数集)
- asp.net ajax调用Web服务