【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 ...
随机推荐
- iOS-RunLoop,为手机省电,节省CPU资源,程序离不开的机制
RunLoop是什么?基本操作是什么? 1.RunLoop的作用 RunLoop可以: 保持程序的持续运行 处理App中的各种事件(比如触摸事件.定时器事件.Selector事件) 节省CPU资源,提 ...
- 一个简单的获取参数的jqure
今天做项目的时候需要用到上一页面传递过来的参数(只要一个参数),其解决办法就是下面: char latter=location.search.split('=')[1] 以上直接获取到第一个参数的值为 ...
- Qt 5 在Windows下 出现QApplication: No such file or directory 问题的解决办法
解决方法是:在*.pro工程项目文件中添加一行QT += widgets,然后再编译运行就OK了.
- netstat命令[转]
原文地址:http://www.cnblogs.com/peida/archive/2013/03/08/2949194.html netstat命令用于显示与IP.TCP.UDP和ICMP协议相关的 ...
- c/c++面试总结(1)
最近在找新的工作,在找工作中遇到很多面试题,大多数让我很难堪,再次让我认识到自己的知识的匮乏,上份工作是以应届生的身份,所有当时进项目组也没有很多要求,进入项目组后自己还算好学(自己以为),之前也没有 ...
- 用CSS实现Firefox 和IE 都支持的Alpha透明效果
有的时候,为了实现一些特殊效果,需要将页面元素变透明,本文介绍的就是用 CSS 实现 Firefox 和 IE 都支持的 Alpha 透明效果.CSS: filter:alpha(opacity=50 ...
- ASP.NET MVC + EF 利用存储过程读取大数据,1亿数据测试很OK
看到本文的标题,相信你会忍不住进来看看! 没错,本文要讲的就是这个重量级的东西,这个不仅仅支持单表查询,更能支持连接查询, 加入一个表10W数据,另一个表也是10万数据,当你用linq建立一个连接查询 ...
- Android混淆问题
最近做了2个项目,全部要混淆,刚接触,自己在网上找了还多资料,感觉各有千秋,自己总结了一下,第一次发帖,不喜勿喷.求各种指导!!! android应用程序的混淆打包规范 1.在工程文件project. ...
- nginx反向代理的配置优化
作者:守住每一天 blog:liuyu.blog.51cto.combbs:bbs.linuxtone.orgmsn:liuyubj520#hotmail.comemail:liuyu105#gmai ...
- Spannable相关方法
实现文本链接 其中tv是TextView类型的控件.只需写java代码即可实现链接,无需在xml文件中进行其他的设置. SpannableString spanTxt = new SpannableS ...