【WCF--初入江湖】12 WCF与Ajax编程
12 WCF与Ajax编程
Ajax
Ajax基本原理
AJAX技术的本质原理就是:使用 JavaScript 的 XMLHttpRequest 对象来直接与服务器进行通信。
通过这个对象,JavaScript 可在不重载页面的情况与 Web 服务器交换数据。然后通过DOM更新部分页面数据。
Ajax最大的优点
在于它带来的更好的用户体验。传统的web页面请求不同,AJAX技术当提交表单时就向web服务器发送一个请求。
服务器接收并处理传来的表单,然後返回一个新的网页。这个做法浪费了许多带宽,
因为在前後两个页面中的大部分HTML代码往往是相同的。AJAX 在浏览器与 Web 服务器之间使用异步数据传输(HTTP 请求),
这样就可使网页从服务器请求少量的信息,而不是整个页面。
ajax调用WCF
WCF服务,为了能使js调用,必须为服务实现类(因为AspNetCompatibilityRequirements只对类有效)设置AspNetCompatibilityRequirements为Allowed,使得
WCF跟AspNet的技术相兼容。
WCF虽然在设计上可以进行独立传输,但当应用于一个ASP.NET AJAX应用程序环境下时,
WCF服务实际上可以工作在一种十分类似于ASMX服务的方式下。
借助于这个AspNetCompatibilityRequirements属性,我们可以指示WCF使用与ASMX服务相同的模型进行工作。
注意,要实现WCF服务与ASMX服务的兼容性至少要实现两点。
第一,在配置文件web.config中,需要进行类似如下的声明式定义:
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
</system.serviceModel>
第二,开发者需要显式地选择一个给定WCF服务的兼容性方式,这是通过使用服务AspNetCompatibilityRequirements属性完成的。
新建一个WCF应用程序项目:
IServer.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Security.Cryptography.X509Certificates;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text; namespace Keasy5.WCF.Ajax.Service
{
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetData(string value);
}
}
Service1.svc.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;
using Keasy5.WCF.Ajax.Service; namespace Keasy5.WCF.Ajax.Service
{
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1 : IService1
{
public string GetData(string value)
{
return string.Format("Hi:{0}", value);
}
}
}
服务端配置:
<?xml version="1.0" encoding="utf-8"?>
<configuration> <system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors> <endpointBehaviors>
<behavior name="enableWebScript_ajax">
<enableWebScript />
</behavior>
</endpointBehaviors> </behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true"
aspNetCompatibilityEnabled="true"/> <services>
<service name="Keasy5.WCF.Ajax.Service.Service1">
<endpoint address=""
behaviorConfiguration="enableWebScript_ajax"
binding="webHttpBinding"
contract="Keasy5.WCF.Ajax.Service.IService1"></endpoint>
</service>
</services> </system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
</system.webServer> </configuration>
右键Service1.svc--在浏览器中查看,在原来的地址中加入“/js”,修改地址为:
http://localhost:18476/Service1.svc/js
WCF 服务返回:
Type.registerNamespace('tempuri.org');
tempuri.org.IService1=function() {
tempuri.org.IService1.initializeBase(this);
this._timeout = ;
this._userContext = null;
this._succeeded = null;
this._failed = null;
}
tempuri.org.IService1.prototype={
_get_path:function() {
var p = this.get_path();
if (p) return p;
else return tempuri.org.IService1._staticInstance.get_path();},
GetData:function(value,succeededCallback, failedCallback, userContext) {
return this._invoke(this._get_path(), 'GetData',false,{value:value},succeededCallback,failedCallback,userContext); }}
tempuri.org.IService1.registerClass('tempuri.org.IService1',Sys.Net.WebServiceProxy);
tempuri.org.IService1._staticInstance = new tempuri.org.IService1();
tempuri.org.IService1.set_path = function(value) { tempuri.org.IService1._staticInstance.set_path(value); }
tempuri.org.IService1.get_path = function() { return tempuri.org.IService1._staticInstance.get_path(); }
tempuri.org.IService1.set_timeout = function(value) { tempuri.org.IService1._staticInstance.set_timeout(value); }
tempuri.org.IService1.get_timeout = function() { return tempuri.org.IService1._staticInstance.get_timeout(); }
tempuri.org.IService1.set_defaultUserContext = function(value) { tempuri.org.IService1._staticInstance.set_defaultUserContext(value); }
tempuri.org.IService1.get_defaultUserContext = function() { return tempuri.org.IService1._staticInstance.get_defaultUserContext(); }
tempuri.org.IService1.set_defaultSucceededCallback = function(value) { tempuri.org.IService1._staticInstance.set_defaultSucceededCallback(value); }
tempuri.org.IService1.get_defaultSucceededCallback = function() { return tempuri.org.IService1._staticInstance.get_defaultSucceededCallback(); }
tempuri.org.IService1.set_defaultFailedCallback = function(value) { tempuri.org.IService1._staticInstance.set_defaultFailedCallback(value); }
tempuri.org.IService1.get_defaultFailedCallback = function() { return tempuri.org.IService1._staticInstance.get_defaultFailedCallback(); }
tempuri.org.IService1.set_enableJsonp = function(value) { tempuri.org.IService1._staticInstance.set_enableJsonp(value); }
tempuri.org.IService1.get_enableJsonp = function() { return tempuri.org.IService1._staticInstance.get_enableJsonp(); }
tempuri.org.IService1.set_jsonpCallbackParameter = function(value) { tempuri.org.IService1._staticInstance.set_jsonpCallbackParameter(value); }
tempuri.org.IService1.get_jsonpCallbackParameter = function() { return tempuri.org.IService1._staticInstance.get_jsonpCallbackParameter(); }
tempuri.org.IService1.set_path("http://localhost:18476/Service1.svc");
tempuri.org.IService1.GetData= function(value,onSuccess,onFailed,userContext) {tempuri.org.IService1._staticInstance.GetData(value,onSuccess,onFailed,userContext); }
可以看成是服务端调用的代理类。
示例1:用户验证
示例2:滚动图片新闻
【The end】
【WCF--初入江湖】12 WCF与Ajax编程的更多相关文章
- WCF分布式开发步步为赢(12):WCF事务机制(Transaction)和分布式事务编程
今天我们继续学习WCF分布式开发步步为赢系列的12节:WCF事务机制(Transaction)和分布式事务编程.众所周知,应用系统开发过程中,事务是一个重要的概念.它是保证数据与服务可靠性的重要机制. ...
- WCF分布式开发步步为赢(4):WCF服务可靠性传输配置与编程开发
今天继续WCF分布式开发步步为赢系列的第4节:WCF服务可靠性传输配置与编程开发.这个章节,我们要介绍什么是WCF服务的可靠性传输,随便介绍网络协议的概念,Web Service为什么不支持可靠性传出 ...
- http服务&ajax编程
http服务&ajax编程 1.服务器 前言:通俗的讲,能够提供某种服务的机器(计算机)称为服务器 1.1.服务器类型 按照不同的划分标准,服务可划分为以下类型: 按服务类型可分为:文件服务器 ...
- 大比速:remoting、WCF(http)、WCF(tcp)、WCF(RESTful)、asp.net core(RESTful)
近来在考虑一个服务选型,dotnet提供了众多的远程服务形式.在只考虑dotnet到dotnet的情形下,我们可以选择remoting.WCF(http).WCF(tcp).WCF(RESTful). ...
- 大比速:remoting、WCF(http)、WCF(tcp)、WCF(RESTful)、asp.net core(RESTful) .net core 控制台程序使用依赖注入(Autofac)
大比速:remoting.WCF(http).WCF(tcp).WCF(RESTful).asp.net core(RESTful) 近来在考虑一个服务选型,dotnet提供了众多的远程服务形式.在只 ...
- 01HTTP服务&AJAX编程
HTTP服务&AJAX编程 一.服务器 1. 什么是服务器? 能够提供某种服务的机器(计算机)称为服务器. 2.服务器的分类: 1.按系统分类:Lin ...
- WCF服务与WCF数据服务的区别
问: Hi, I am newbie to wcf programming and a little bit confused between WCF Service and WCF Data Se ...
- AJAX编程-封装ajax工具函数
即 Asynchronous [e'sɪŋkrənəs] Javascript And XML,AJAX 不是一门的新的语言,而是对现有技术的综合利用.本质是在HTTP协议的基础上以异步的方式与服务器 ...
- Ajax编程中,经常要能动态的改变界面元素的样式
在Ajax编程中,经常要能动态的改变界面元素的样式,可以通过对象的style属性来改变,比如要改变背景色为红色,可以这样写:element.style.backgroundColor=”#ff0000 ...
随机推荐
- AMQ学习笔记 - 05. 客户端模板化
概述 客户端编程模型中,大部分的步骤都是相同的.将相同的部分做成模板,将不同的部分预留接口,实现者就只需要针对不同的部分提供实现. 设计 类图 发送方客户端 说明: 基于模板的思想,SendTempl ...
- JAVA:类的三大特征,抽象类,接口,final关键字<3>
一.类的三大特征 1.封装性 (1).什么是封装 封装就是把抽象出的数据和对数据的操作封装在一起, 数据被保护在内部, 程序的其他部分只有通过被授权的操作(成员方法), 才能对数据进行操作. (2). ...
- php魔术方法 http_build_query使用
最近在做一个项目使用到 http_build_query 这个魔术方法很好用,它可以将一个数组转换成这样的格式: 比如 $_arr = array('action'=>'show','page' ...
- mysql之触发器trigger(1)
触发器(trigger):监视某种情况,并触发某种操作. 触发器创建语法四要素:1.监视地点(table) 2.监视事件(insert/update/delete) 3.触发时间(after/befo ...
- webstorm 添加文件模板
Ctrl+Shift+A 搜索设置 File Teamplate 添加 File Teamplate
- Vue.js 2.0版
Vue.js 2.0版升级,更改了好多方法或指令 new Vue({ el:'#demo', data:{ msg:"vue2.0" } }) v-model lazy numbe ...
- 层叠水平(stacking level)
运用上图的逻辑,上面的题目就迎刃而解,inline-blcok 的 stacking level 比之 float 要高,所以无论 DOM 的先后顺序都堆叠在上面. 不过上面图示的说法有一些不准确,按 ...
- 有关Mysql连接问题
问题一——Mysql number Error 2003 MySQL连接错误.错误代码10061.10061一般是Mysql服务没启动.或Mysql服务器无法连接 . 在程序栏找到Mysql\Mysq ...
- 亚马逊左侧导航(jquery.menuaim.js)
jquery.menuaim.js 主菜单 <div class="active"> <ul class="dropdown-menu" ...
- Python数据结构——栈、队列的实现(一)
1. 栈 栈(Stack)是限制插入和删除操作只能在一个位置进行的表,该位置是表的末端,称为栈的顶(top).栈的基本操作有PUSH(入栈)和POP(出栈).栈又被称为LIFO(后入先出)表. 1.1 ...