在 ASP.NET 3.5 中使用同时支持 Ajax Json 和 .NET 客户端的 WCF 服务

  近期的开发要求使用 IIS 承载的 WCF 提供数据和实现操作,客户端使用 Ajax Json 和 WinForm,因此要在统一服务上同时绑定用于 Ajax 的 webHttpBinding 和用于 WinForm 的 wsHttpBinding。

  下面假定服务名为 Itoro.WebCore.Web。

  在创建服务时,选择“启用了 AJAX 的 WCF 服务”,然后在 web.config 会自动在 system.serviceModel 下生成相关配置,默认的用于 Ajax 的配置是行为中的名为 Itoro.WebCore.WebAspNetAjaxBehavior 的终点行为,并且使用 <enableWebScript /> 启用 Ajax Javascript 支持。在服务配置中是一个名为 Itoro.WebCore.Web 的服务,其中的终点配置为使用 Itoro.WebCore.WebAspNetAjaxBehavior 行为并绑定到 webHttpBanding。

  接下来是手动修改配置以支持 WinForm 的客户端访问,即 wsHttpBinding。

  在 system.serviceModel/behaviors/serviceBehaviors 下添加如下内容(如不存在 serviceBehaviors 则在相应位置创建该元素):

< behavior  name ="Itoro.WebCore.WebBehavior" >
    
< serviceMetadata  httpGetEnabled ="true"   />
</ behavior >

  该配置指定名为 Itoro.WebCore.WebBehavior 的服务行为启用 Http 下的服务元数据。

  接下来修改名为 Itoro.WebCore.Web 的服务(即 <service name="Itoro.WebCore.Web" /> 元素)为以下内容:

< service  behaviorConfiguration ="Itoro.WebCore.WebBehavior"  name ="Itoro.WebCore.Web" >
    
< endpoint  address ="web"  behaviorConfiguration ="Itoro.WebCore.WebAspNetAjaxBehavior"
        binding
="webHttpBinding"  contract ="Itoro.WebCore.Web"   />
    
< endpoint  address ="ws"  binding ="wsHttpBinding"  contract ="Itoro.WebCore.Web" >
        
< identity >
            
< dns  value ="localhost" />
        
</ identity >
    
</ endpoint >
    
< endpoint  address ="mex"  binding ="mexHttpBinding"  contract ="IMetadataExchange" />
</ service >

  其中添加 service 元素中的 behaviorConfiguration,添加了2个终点配置,分别为用于 WinForm 的 wsHttpBinding 和用于元数据的 mexHttpBinding。另外,修改 webHttpBinding 和 wsHttpBinding 的 address 属性分别的 web 和 ws(也可以自定义,但是一定要设置,而且不能相同,否则会因为不同类型的绑定监听同一 Uri 而发生错误)。

  完整的配置如下:

< system.serviceModel >
    
< behaviors >
        
< endpointBehaviors >
            
< behavior  name ="Itoro.WebCore.WebAspNetAjaxBehavior" >
                
< enableWebScript  />
            
</ behavior >
        
</ endpointBehaviors >
        
< serviceBehaviors >
            
< behavior  name ="Itoro.WebCore.WebBehavior" >
                
< serviceMetadata  httpGetEnabled ="true"   />
            
</ behavior >
        
</ serviceBehaviors >
    
</ behaviors >
    
< serviceHostingEnvironment  aspNetCompatibilityEnabled ="true"   />
    
< services >
        
< service  behaviorConfiguration ="Itoro.WebCore.WebBehavior"  name ="Itoro.WebCore.Web" >
            
< endpoint  address ="web"  behaviorConfiguration ="Itoro.WebCore.WebAspNetAjaxBehavior"
                binding
="webHttpBinding"  contract ="Itoro.WebCore.Web"   />
            
< endpoint  address ="ws"  binding ="wsHttpBinding"  contract ="Itoro.WebCore.Web" >
                
< identity >
                    
< dns  value ="localhost" />
                
</ identity >
            
</ endpoint >
            
< endpoint  address ="mex"  binding ="mexHttpBinding"  contract ="IMetadataExchange" />
        
</ service >
    
</ services >
</ system.serviceModel >

  最后在服务使用上也要有些变动,假设我们的服务为 http://localhost/Web.svc,那么在 Ajax 网站上添加服务时应该使用 Web.svc/web,而生成的脚本引用地址为 Web.svc/web/js,其中的 web 即为 webHttpBinding 中的 address。在 WinForm 中可以直接通过 http://localhost/Web.svc 添加服务引用,在生成的 app.config 中 /configuration/system.serviceModel/client 下会有2个终点配置,其中一个是 WebHttpBinding_Web,另外一个是 WSHttpBinding_Web,因此在使用时必须指定终点为 WSHttpBinding_Web,即 WebClient client = new WebClient("WSHttpBinding_Web") 否则运行时会出现错误。

  最后,因为 Web 是无状态的,而 WinForm 是有状态的,所以在 Web 下需要使用 Cookie(包括 Session),而在 WinForm 下只要 WebClient client 没有销毁就会一直保留同一状态,因为在编写服务的时候也要考虑兼容这两种模式。

转载于:https://www.cnblogs.com/hust21941/archive/2008/09/03/wcf-config-ajax-winform.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值