`

WCF系列(一)【翻译】BasicHttpBinding 和 WsHttpBinding 的不同点

阅读更多

 

原文地址:Difference between BasicHttpBinding and WsHttpBinding

 

1、简介

  WCF引入了很多的绑定和协议。本文重点讨论两个协议,BasicHttpBinding和WsHttpBinding,他们看起来很相似,但是却有很大的不同。因此,我们首先看一下他们的不同点,然后通过一个小项目看看他们到底有什么不同。

  作者还总结了400多个.NET相关的话题,例如:WCF,WPF,WWF,Ajax,Core .NET,SQL Server,Architecture等等。

  下载地址:/Files/virusswb/SampleDotNetInterviewQuestionBook.zip

2、预备知识

  如果你第一次接触WCF,可以通过下面的链接了解一下相关的知识。在本文就不讲述WCF的基础知识点了:

3、BasicHttpBinding和WsHttpBinding的不同点

  如果非要用一句话概述BasicHttpBinding和WsHttpBinding的不同的话,那就是WsHttpBinding支持WS-Security specifications,WS-Security specifications具有扩展web service的能力。

  下面的表格式是对两者在安全、兼容性、可靠性和SOAP版本方面的比较。

  

Criteria BasicHttpBinding WsHttpBinding
Security support This supports the old ASMX style, i.e. WS-BasicProfile 1.1. This exposes web services using WS-* specifications.
Compatibility This is aimed for clients who do not have .NET 3.0 installed and it supports wider ranges of clients. Many of the clients like Windows 2000 still do not run .NET 3.0. So older version of .NET can consume this service. As its built using WS-* specifications, it does not support wider ranges of client and it cannot be consumed by older .NET version less than 3 version.
Soap version SOAP 1.1 SOAP 1.2 and WS-Addressing specification.
Reliable messaging Not supported. In other words, if a client fires two or three calls you really do not know if they will return back in the same order. Supported as it supports WS-* specifications.
Default security options By default, there is no security provided for messages when the client calls happen. In other words, data is sent as plain text. As WsHttBinding supports WS-*, it has WS-Security enabled by default. So the data is not sent in plain text.
Security options
  • None
  • Windows – default authentication
  • Basic
  • Certificate
  • None
  • Transport
  • Message
  • Transport with message credentials

 

  两者之间最大的不同你一定已经注意到了,那就是安全。默认情况下,BasicHttpBinding发送的是明文数据,而WsHttpBinding发送的是加密和更加安全的数据。为了证明这一点,我们新建两个服务,一个使用BasicHttpBinding,一个使用WsHttpBinding,然后详细查看一下他们的安全方面。

  我们创建一个小例子,看看basicHttpBinding是如何明文发送数据的,wsHttpBinding是如何加密数据的。

  说明:默认情况下,使用basicHttpBinding的时候,安全是没有启用的。换句话说,它很像以前的webservice,也就是.asmx。但是不意味着我们不能启用安全。稍后,我会写一篇关于basicHttpBinding启用安全的文章

  

4、通过5步比较他们的不同点

  为了它们之间实际的不同点,我们创建一个小工程。在工程中,创建两个服务,一个使用basicHttpBinding,一个使用wsHttpBinding。

  

  第一步:使用basicHttpBinding创建一个服务,system.serviceModel配置如下

  

代码
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->  <system.serviceModel>
    
<services>
      
<service name="WCFBasicHttpBinding.Service1" behaviorConfiguration="WCFBasicHttpBinding.Service1Behavior">
        
<!-- Service Endpoints -->
        
<endpoint address="" binding="basicHttpBinding" contract="WCFBasicHttpBinding.IService1">
          
<!-- 
              Upon deployment, the following identity element should be removed or replaced to reflect the 
              identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity 
              automatically.
          
-->
          
<identity>
            
<dns value="localhost"/>
          
</identity>
        
</endpoint>
        
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      
</service>
    
</services>
    
<behaviors>
      
<serviceBehaviors>
        
<behavior name="WCFBasicHttpBinding.Service1Behavior">
          
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          
<serviceMetadata httpGetEnabled="true"/>
          
<!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          
<serviceDebug includeExceptionDetailInFaults="false"/>
        
</behavior>
      
</serviceBehaviors>
    
</behaviors>
  
</system.serviceModel>

 

 

  第二步:创建一个WsHttpBinding的服务,配置如下

  

代码
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />--><system.serviceModel>
    
<services>
      
<service name="WCFWsHttpBindingHttps.Service1" behaviorConfiguration="WCFWsHttpBindingHttps.Service1Behavior">
        
<!-- Service Endpoints -->
        
<endpoint address="" binding="wsHttpBinding" contract="WCFWsHttpBindingHttps.IService1">
          
<!-- 
              Upon deployment, the following identity element should be removed or replaced to reflect the 
              identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity 
              automatically.
          
-->
          
<identity>
            
<dns value="localhost"/>
          
</identity>
        
</endpoint>
        
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      
</service>
    
</services>
    
<behaviors>
      
<serviceBehaviors>
        
<behavior name="WCFWsHttpBindingHttps.Service1Behavior">
          
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          
<serviceMetadata httpGetEnabled="true"/>
          
<!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          
<serviceDebug includeExceptionDetailInFaults="false"/>
        
</behavior>
      
</serviceBehaviors>
    
</behaviors>
  
</system.serviceModel>

 

  第三步:我们不创建任何新函数,就是用默认创建的两个函数,如下

  

代码
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />--> public class Service1 : IService1
    {
        
public string GetData(int value)
        {
            
return string.Format("You entered: {0}", value);
        }

        
public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            
if (composite.BoolValue)
            {
                composite.StringValue 
+= "Suffix";
            }
            
return composite;
        }
    }

 

 

  第四步:服务已经创建好了,我们创建一个消费服务的客户端。在这里,我们创建一个WebApplication,添加两个引用,一个是service reference,WsHttpBinding;另外一个是web reference,BasicHttpBinding。请记住,在你右键添加引用的时候,通过service reference添加WsHttpBinding,通过web reference添加BasicHttpBinding。

  

  

  我们在webapplication的default页面上添加两个button,一个调用HTTP Service,另外一个调用wshttp service。下面是它们如何调用服务的GetData方法。

  

  第五步:到这里我们准备完成这个项目,到了嗅探的时候了,看看数据在客户端和两个服务之间是如何传输的。我们下载并使用HTTP数据记录器,IE Inspector。我们将一个一个的点击button,来记录数据的传输。你将会看到在basicHttpBinding的情况下,数据明文的通过xml发送;在wsHttpBinding的情况下,数据被加密发送。

  

  总之,尽量避免使用BasicHttpBinding。

5、什么时候使用BasicHttpBinding,什么时候使用WsHttpBinding

  如果你希望有向后兼容的能力,并且支持更多的客户端,你可以选择basicHttpBinding,如果你确定你的客户端使用的是.NET 3.0甚至更高的话,你可以选择wsHttpBinding。

分享到:
评论

相关推荐

    silverlight与WCF通信简单例子

    4. **绑定选择**:Silverlight与WCF通信时,可以选择不同的绑定类型,如BasicHttpBinding、WsHttpBinding等。考虑到Silverlight的限制,通常会选择BasicHttpBinding,因为它不支持WS-Security等高级特性,但能满足...

    A.4 WCF和HTTP文件传输练习

    WCF是一种全面的服务导向架构,它允许开发者创建强类型、面向服务的应用程序,这些应用程序能够跨不同的平台和网络进行通信。WCF支持多种传输协议,如HTTP、TCP、命名管道等,同时提供了丰富的安全性和事务处理机制...

    WCF同时支持AJAX和SOAP进行调用

    WCF的一个显著特性是它同时支持AJAX(Asynchronous JavaScript and XML)和SOAP(Simple Object Access Protocol)这两种不同的调用方式,为开发人员提供了更多的灵活性。 **AJAX调用**: AJAX是一种在不重新加载...

    wcf揭秘英文版 wcf揭秘英文版

    WCF提供了多种内置绑定,如BasicHttpBinding、WSHttpBinding和NetTcpBinding,每种都有其特定的应用场景和性能特征。 3. **终结点**:服务通过终结点与外界交互,每个终结点由地址、绑定和契约组成。地址指定服务的...

    老老实实wcf第一篇

    WCF融合了多种不同的分布式应用编程模型,如ASMX Web Services、WSE Web Services、MSMQ、DCOM等,使得开发者能够更容易地开发跨平台和跨语言的服务。 #### 二、WCF的核心概念 为了更好地理解和使用WCF,首先需要...

    wcf揭秘中文清晰版

    常见的绑定有BasicHttpBinding、WSHttpBinding、NetTcpBinding等,每种绑定支持不同的安全、可靠性及性能特性。 4. **合同**:合同定义了服务提供的操作、消息结构以及服务接口。服务合同可以是数据合同、操作合同...

    基于WCF创建SOAP和REST服务源码

    在IT行业中,Windows Communication Foundation(WCF)是微软推出的一种用于构建分布式应用程序的服务框架,它集成了多种通信协议,如SOAP(简单对象访问协议)和REST(Representational State Transfer)。...

    WCF匿名客户端加密传输数据

    在WCF中,可以设置绑定元素(如BasicHttpBinding、WsHttpBinding等)的UseDefaultWebProxy属性为true,并使用Transport或TransportWithMessageCredential安全模式,结合HTTPS协议来启用SSL/TLS。 在提供的压缩文件...

    VS10创建WCF教程

    在WCF中,有多种预定义的Binding可供选择,如NetTcpBinding(高效,适用于内部网络)、WsHttpBinding(支持互操作性,安全性较强)和BasicHttpBinding(简单,用于兼容旧版Web服务)等。每种绑定都对应特定的传输...

    WCF一分钟学会入门实例

    Windows Communication Foundation(WCF)是微软.NET框架中用于构建服务的一种技术,它允许开发者创建分布式应用程序,实现不同系统之间的通信。本入门实例旨在让你在短时间内理解并掌握WCF的基础概念和操作。 首先...

    WCF入门教程,我的第一个WCF

    1. **服务**: WCF服务是对外提供功能的实体,可以通过不同的绑定和地址被访问。服务由接口定义,实现该接口的类是服务的实现。 2. **终结点**: 服务通过终结点与外部世界交互,每个终结点包含地址、绑定和合同。...

    silverlight和wcf不在同一个域中(http,tcp协议均可)

    标题中的“Silverlight和WCF不在同一个域中”是指在开发Web应用程序时,Silverlight客户端与Windows Communication Foundation (WCF)服务之间的通信可能涉及到跨域问题。这是因为Silverlight应用通常运行在用户...

    WCF通讯例子

    **WCF(Windows Communication Foundation)**是微软.NET框架下的一种面向服务的通信框架,它提供了一种统一的方式来创建和部署可互操作的、安全的、可靠的服务。在本例中,我们将探讨WCF如何实现HTTP和SOAP两种通信...

    wcf4.0英文教材

    根据提供的文件信息,我们可以归纳出一系列与WCF 4.0相关的专业知识点。这份教材主要围绕Windows Communication Foundation (WCF) 4.0展开,详细介绍了如何利用.NET 4进行服务开发、部署和服务交互等内容。下面我们...

    wcf 和restful整个mvc的框架demo

    WCF服务可以通过配置文件定义服务接口、行为、绑定和终结点,为开发者提供了高度的灵活性。 RESTful服务基于HTTP协议,强调资源的表示和状态转移。它通过HTTP方法(GET、POST、PUT、DELETE)来操作资源,使得客户端...

    一个简单的WCF例子

    **WCF(Windows Communication Foundation)**是微软.NET框架下的一种面向服务的通信技术,它提供了一种统一的方式来构建分布式应用程序,允许不同的系统和服务之间进行安全、可靠、可管理的通信。在本文中,我们将...

    动态wcf.rar

    总结来说,动态WCF是.NET开发中的一个高级特性,它允许开发者在运行时创建和管理WCF服务,极大地增强了服务的灵活性和适应性。在Silverlight应用中,动态WCF能够实现跨域数据通信,通过异步调用和合适的绑定方式优化...

    C#编写的WCF服务项目

    本项目提供了一个使用C#编写的WCF服务示例,适用于Visual Studio 2008及更高版本,便于理解和学习。 1. **WCF服务基础** - WCF是微软提出的一种统一的编程模型,它集成了.NET Framework中的Web服务(ASMX)、...

    wcf 服务编程第六章

    WCF提供了多种预定义的绑定,如BasicHttpBinding、WSHttpBinding、NetTcpBinding等。每种绑定都支持不同的传输协议、身份验证机制和消息编码。开发者也可以自定义绑定以满足特定需求。 ### 4. 端点 服务端点是服务...

    WCF培训资源.WCF培训资源

    WCF 作为一种强大的服务通信框架,不仅支持多种消息格式和通信协议,还内置了对事务处理、安全性和可靠性等功能的支持,非常适合构建复杂的分布式应用程序。通过对 WCF 的深入学习和掌握,开发人员可以更有效地构建...

Global site tag (gtag.js) - Google Analytics