jquery调用wcf需要注意的一些问题
用jquery直接调用wcf是更好的选择。在尝试这种方式的过程中遇到的一些问题和一些需要注意的地方需要记录一下,所以就写了这篇随笔。
xland的jquery调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. 
   二、客户端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/>
                    
                
                
            
        
浙公网安备 33010602011771号