昨天,博客园首页增加了Digg功能。在该功能中我们开始尝试使用jQuery直接调用WCF。之前我们采用的方案是jQuery调用Web Service,然后WebService再调用服务层。这样调用主要是因为之前需要调用不同域名下的WCF服务,因为跨域调用的问题,就要通过Web Service中转一下。而这次Digg功能调用的是同一个应用程序下的WCF,用jQuery直接调用WCF是更好的选择。在尝试这种方式的过程中遇到的一些问题和一些需要注意的地方需要记录一下,所以就写了这篇随笔。

  xlandjQuery调WCF给了我们很大帮助,在这里感谢xland!在探索技术的过程中,将自己解决问题的经验记录下来,不仅可以备忘、总结,而且可以帮助遇到同样问题的朋友,这也是写博客的一种乐趣吧。

  进入正题,jQuery调用WCF需要注意的一些问题:

  1. WCF的配置(WCF服务宿主于IIS 7)

  1)WCF服务相关配置:

  在需要调用的接口方法(OperationContract)上加上属性[WebInvoke(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)],比如:


    [ServiceContract] 
    public interface IDiggService
    {      
        [OperationContract]
        [WebInvoke(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
        string GetDiggCountList(string entryIdList);
    }

  给服务实现类加上属性:

 [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class DiggService : IDiggService
    {
     }

  否则调用时会出现错误:“IIS 7.0 Detailed Error - 500.0 - System.ServiceModel.ServiceActivationException”。

2) Web.config中的WCF相关配置:


<system.serviceModel>
    <services>  
      <service name="DiggService">
        <endpoint address="" binding="webHttpBinding" contract="IDiggService" behaviorConfiguration="DiggServiceBehavior"/>
      </service>
    </services>
    <behaviors>     
      <endpointBehaviors>
        <behavior name="DiggServiceBehavior">
          <enableWebScript/>          
        </behavior>
      </endpointBehaviors>
    </behaviors>
</system.serviceModel>

  需要注意两个地方的配置:

  a)  binding="webHttpBinding",开始设置为binding="basicHttpBinding",出现错误提示:

The endpoint at 'DiggService.svc' does not have a Binding with the None MessageVersion.  'System.ServiceModel.Description.WebScriptEnablingBehavior' is only intended for use with WebHttpBinding or similar bindings.

b)  <enableWebScript/> ,启用这个设置才能让WCF支持Ajax调用,否则调用时WCF会返回这样的错误:

The message with To 'DiggService.svc/GetDiggCountList' cannot be processed at the receiver, due to an AddressFilter mismatch at the EndpointDispatcher. Check that the sender and receiver's EndpointAddresses agree.

c) 当出现 如下错误
 使用“UriTemplate”的终结点无法用于“System.ServiceModel.Description.WebScriptEnablingBehavior”。 
把 <enableWebScript/>换成<webHttp/>,解决!

  二、客户端jQuery调用注意

  开始按照调用Web Servcie的方式调用:


    $.ajax({
        url: '/wcf/DiggService.svc/GetDiggCountList',
        data: '{"entryIdList":"' + entryIdList + '"}',
        type: 'post',
        dataType: 'json',
        contentType: 'application/json; charset=utf8',
        success: function(data) {
            if (data.d) {
                          }
            }
        },
        error: function(xhr) {
            alert(xhr.responseText);
        }
    });  

  在FireFox中能成功调用,但在IE 8和Google Chrome中,调用后返回的却是IIS 7的错误信息:IIS 7.0 Detailed Error - 400.0 - Bad Request。

  后来将 contentType: 'application/json; charset=utf8' 改为 contentType: 'text/json'问题就解决了。

  jQuery调用Web Service与WCF还有一个不同之处在参数格式的处理上:

  比如上面代码中的data: '{"entryIdList":"' + entryIdList + '"}',如果将参数名的双引号去掉,即data: '{entryIdList:"' + entryIdList + '"}',可以正常调用Web Service,调用WCF会出现Json反序列化的异常。

  三、其他需要注意的地方

  如果WCF服务所在的IIS站点中绑定了多个域名, 在没有设置baseAddressPrefixFilters的情况下,会出现错误提示:

This collection already contains an address with scheme http.  There can be at most one address per scheme in this collection.Parameter name: item

设置好baseAddressPrefixFilters,比如:

    <serviceHostingEnvironment>
      <baseAddressPrefixFilters>
        <add prefix="http://www.cnblogs.com"/>
      </baseAddressPrefixFilters>
    </serviceHostingEnvironment> 

这样在访问http://www.cnblogs.com时能正常调用,但访问http://cnblogs.com时调用就出错(IIS 7.0 Detailed Error - 404.0 - Not Found),因为之前的代码中使用的是相对地址调用,实际调用的是http://cnblogs.com/wcf/DiggService.svc/GetDiggCountList,由于设置了baseAddressPrefixFilters,不允许这样调用,只能改为绝对地址(http://www.cnblogs.com/wcf/DiggService.svc/GetDiggCountList),这样又会造成跨域调用。这个问题目前还不知如何解决。

  四、遗留问题

  如何控制缓存,比如:如何在WCF返回时设置Expires Header和If-Modified-Since,避免频繁的WCF调用。

  五、总结

  jQuery调用WCF的要点:

  1. [WebInvoke(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]

  2. [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

  3.  binding="webHttpBinding"

  4. <enableWebScript/>

  5.  contentType: 'text/json'

  参考文章:

  1. jQuery调WCF

  2. jQuery Ajax 全解析

  相关小组: http://space.cnblogs.com/group/100319/

转自:http://www.cnblogs.com/dudu/archive/2009/07/14/1523082.html

参考:http://www.cnblogs.com/yjmyzz/archive/2009/11/04/1595783.html

jQuery调用WCF需要注意的一些问题的更多相关文章

  1. VS2010中使用Jquery调用Wcf服务读取数据库记录

    VS2010中使用Jquery调用Wcf服务读取数据库记录 开发环境:Window Servere 2008 +SQL SERVE 2008 R2+ IIS7 +VS2010+Jquery1.3.2 ...

  2. 抛弃WebService,在.NET4中用 jQuery 调用 WCF

    在我们之前的开发中,对于ajax程序,都是通过jQuery调用标记为[System.Web.Script.Services.ScriptService]的WebService,然后在WebServic ...

  3. JQuery调用WCF服务,部署在iis

    Normal 0 7.8 磅 0 2 false false false EN-US ZH-CN X-NONE /* Style Definitions */ table.MsoNormalTable ...

  4. jQuery调用WCF

    jQuery要调用WCF,首先要创建service.svc服务文件,这里边需要注意: [ServiceContract(Namespace = "")] [AspNetCompat ...

  5. jquery调用wcf案例

    ----------根据其他网友总结 1.在契约接口上添加:[WebInvoke(RequestFormat=WebMessageFormat.Json,ResponseFormat=WebMessa ...

  6. jQuery调用WCF服务传递JSON对象

    下面这个示例使用了WCF去创建一个服务端口从而能够被ASP.Net页面通过jQuery的AJAX方法访问,我们将在客户端使用Ajax技术来 与WCF服务进行通信.这里我们仅使用jQuery去连接Web ...

  7. JQuery调用WCF服务

    一:创建一个wcf服务项目 [ServiceContract] public interface IService1 { [OperationContract] [WebInvoke(RequestF ...

  8. jQuery调用WCF 说明

    在项目中用过一些WCF的技术这篇文章是对以前用过的一点东西的一个梳理 一,webconfig的配置除了一般的配置外,与WCF相关的配置如下 <system.serviceModel>    ...

  9. jquery 调用wcf 的SOA架构,将三层架构运用到SOA的架构中来(第四天)

    经过前面3天的学习,我想大家应该对SOA的架构有了初步的了解,其实 SOA与三层架构并不冲突,而是三层架构的升级版. 来看下传统的三层架构! 一共可以分为4个层: 模型层(可有可无),客户端,服务端, ...

随机推荐

  1. Python下读取转换unicode的json格式

    转自: https://blog.csdn.net/felcon/article/details/38524317 JSON(JavaScript Object Notation) 是一种轻量级的数据 ...

  2. BZOJ 1975: [Sdoi2010]魔法猪学院 大水题 第k短路 spfa

    https://www.lydsy.com/JudgeOnline/problem.php?id=1975 我好像到现在了第k短路都不会写,mdzz. 先spfa求出最短路,然后扫点存各种前置路径已经 ...

  3. BZOJ.2738.矩阵乘法(整体二分 二维树状数组)

    题目链接 BZOJ 洛谷 整体二分.把求序列第K小的树状数组改成二维树状数组就行了. 初始答案区间有点大,离散化一下. 因为这题是一开始给点,之后询问,so可以先处理该区间值在l~mid的修改,再处理 ...

  4. 构造Huffman以及实现

    构造Huffman 题目 在作业本上分别针对权值集合W=(6,5,3,4,60,18,77)和W=(7,2,4,5,8)构造哈夫曼树,提交构造过程的照片 错误回答 错误原因:遵循左边小于根右边大于根的 ...

  5. zookeeper【1】配置管理

    为什么要用统一配置? 我们做项目时用到的配置比如数据库配置等...我们都是写死在项目里面,如果需要更改,那么也是的修改配置文件然后再投产上去,那么问题来了,如果做集群的呢,有100台机器,这时候做修改 ...

  6. hdoj 5199 Gunner map

    Gunner Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5199 D ...

  7. 推荐C#网站、书籍、资源

    推荐博客: 极简的随笔 http://www.cnblogs.com/guwei4037/p/3499135.html 见证大牛成长之路的专栏 http://blog.csdn.net/shanyon ...

  8. 读书笔记_Effective_C++_条款三十六:绝不重新定义继承而来的non-virtual函数

    这个条款的内容很简单,见下面的示例: class BaseClass { public: void NonVirtualFunction() { cout << "BaseCla ...

  9. 如何设置VMware中Linux命令行环境全屏

    在VMware安装Linux后默认屏幕为640×480,如需修改,则请参考以下步骤.以下以CentOS 6.6安装于VMware Workstation 9中为例说明. 1.默认640x480x16, ...

  10. JDK居然还有Server和Client模式

    JDK这货居然还分Server和Client版本,但经过观察,据说从1.7+版本开始这两者运行的区别已经逐步减少了.所以接下来的分析没啥意义. 参考: http://www.oracle.com/te ...