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编程的更多相关文章

  1. WCF分布式开发步步为赢(12):WCF事务机制(Transaction)和分布式事务编程

    今天我们继续学习WCF分布式开发步步为赢系列的12节:WCF事务机制(Transaction)和分布式事务编程.众所周知,应用系统开发过程中,事务是一个重要的概念.它是保证数据与服务可靠性的重要机制. ...

  2. WCF分布式开发步步为赢(4):WCF服务可靠性传输配置与编程开发

    今天继续WCF分布式开发步步为赢系列的第4节:WCF服务可靠性传输配置与编程开发.这个章节,我们要介绍什么是WCF服务的可靠性传输,随便介绍网络协议的概念,Web Service为什么不支持可靠性传出 ...

  3. http服务&ajax编程

    http服务&ajax编程 1.服务器 前言:通俗的讲,能够提供某种服务的机器(计算机)称为服务器 1.1.服务器类型 按照不同的划分标准,服务可划分为以下类型: 按服务类型可分为:文件服务器 ...

  4. 大比速:remoting、WCF(http)、WCF(tcp)、WCF(RESTful)、asp.net core(RESTful)

    近来在考虑一个服务选型,dotnet提供了众多的远程服务形式.在只考虑dotnet到dotnet的情形下,我们可以选择remoting.WCF(http).WCF(tcp).WCF(RESTful). ...

  5. 大比速: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提供了众多的远程服务形式.在只 ...

  6. 01HTTP服务&AJAX编程

    HTTP服务&AJAX编程 一.服务器         1. 什么是服务器? 能够提供某种服务的机器(计算机)称为服务器. 2.服务器的分类:              1.按系统分类:Lin ...

  7. WCF服务与WCF数据服务的区别

    问: Hi, I am newbie to wcf programming and a little bit confused between WCF Service and WCF Data  Se ...

  8. AJAX编程-封装ajax工具函数

    即 Asynchronous [e'sɪŋkrənəs] Javascript And XML,AJAX 不是一门的新的语言,而是对现有技术的综合利用.本质是在HTTP协议的基础上以异步的方式与服务器 ...

  9. Ajax编程中,经常要能动态的改变界面元素的样式

    在Ajax编程中,经常要能动态的改变界面元素的样式,可以通过对象的style属性来改变,比如要改变背景色为红色,可以这样写:element.style.backgroundColor=”#ff0000 ...

随机推荐

  1. dateset是不是在缓存中

    C#开发erp系统的时候有一个多表数据的查询展示到页面,采用了存储过程的方式,但是存储过程中没有加入分页(菜比).刚开始测试数据几百条没有问题,当数据量提升至十万级后页面加载速度就很卡了,一般是使用分 ...

  2. mysql之触发器trigger(1)

    触发器(trigger):监视某种情况,并触发某种操作. 触发器创建语法四要素:1.监视地点(table) 2.监视事件(insert/update/delete) 3.触发时间(after/befo ...

  3. MySQL 5.7 Zip 安装(win7)

    参考官方文档 http://dev.mysql.com/doc/refman/5.7/en/windows-install-archive.html 2.3.5.1 Extracting the In ...

  4. IPv6协议介绍

    IPv6是为了解决基于IPv4的TCP/IP协议簇遇到的问题而推出的下一代IP协议.由于IPv4中采用的编制方式使得可用的网络地址和主机地址的数目远低于理论数目,随着全球互联网的快速发展,现有的IPv ...

  5. UINavigationController的popViewControllerAnimated问题

    UINavigationController是IOS编程中的一个view controller的容器,通过栈管理viewControllers,每一次push操作都将在栈顶添加一个view contr ...

  6. 使用DriverManager获取数据库连接的一个小改进

    由于使用DriverManager获取数据库连接时,由于DriverManager实现类中有一段静态代码块,可以直接注册驱动,且可以同时管理多个驱动程序 所以当换数据库连接时需要指定不同的数据库,那么 ...

  7. 使用 Time Machine 恢复 .ssh等隐藏文件夹

    重装MAC系统后,要恢复.ssh等文件夹内容,而其在“Finder”中又是默认隐藏的,这时我们可以先在“Finder”中使用“前往文件夹功能…”进入指定文件夹,然后再进入“Time Machine”进 ...

  8. php 读取文件头判断文件类型的实现代码

    php代码实现读取文件头判断文件类型,支持图片.rar.exe等后缀. 例子: <?php $filename = "11.jpg"; //为图片的路径可以用d:/uploa ...

  9. c# 刻度:毫米 英寸 像素转换

    从目前所掌握的资料来看,c#程序中将毫米转换像素的方法无非两种: 第一种: 1: /// <summary> 2: /// 以毫米为单位的显示宽度 3: /// </summary& ...

  10. 获得当前时间的PRO

    1.没有参数的存储过程 create or replace procedure get_timeas    cur_time varchar2(10);begin  select to_char(sy ...