aaaaaaaaaaaaaaaaaa

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

2010-02-21 12:23 by Virus-BeautyCode, 20206 阅读, 7 评论, 收藏, 编辑

原文地址: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的基础知识点了:

  • Windows Communication Framework (WCF) - Part 1
  • Windows Communication Framework (WCF) - Part 2

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配置如下

  

<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的服务,配置如下

  

<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>

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

  

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。

【Blog】http://virusswb.cnblogs.com/

【MSN】jorden008@hotmail.com

【说明】转载请标明出处,谢谢

转载于:https://www.cnblogs.com/wangsea/p/10302047.html

WCF系列(一)BasicHttpBinding 和 WsHttpBinding 的不同点相关推荐

  1. mysql分布式事务wcf_[转载]WCF系列_分布式事务(下)

    浏览到chnking的WCF的分布式事务处理不错,转载过来分享一下.1. WCF分布式事务例子这里也用转账的例子说事. 用户在系统A和系统B都有账户,账户间的资金可以互转,系统A的资金减少多少,系统B ...

  2. wcf系列5天速成——第一天 binding的使用(1)

    作为WCF速成系列,只介绍些项目开发中常用到的实战知识. 学习wcf,还是对其中的几个术语要了解一下.wcf中有一个ABC的概念,就是 第一: "A" 是地址,就是告诉别人我wcf ...

  3. wcf系列---- binding的使用(1)

    文转自http://www.cnblogs.com/huangxincheng/archive/2011/10/23/2221845.html 作为WCF速成系列,只介绍些项目开发中常用到的实战知识. ...

  4. WCF系列(一) -- 完全不使用配置文件构建和使用WCF服务

    只使用代码而不用配置文件的情况不适合IIS为宿主的情况,IIS宿主必须使用配置文件配置WCF的ServiceHost. 1.服务端 1.1.    准备Contract和实现Contract的服务 很 ...

  5. WCF系列(二) -- 使用配置文件构建和使用WCF服务

    当然,配置一个ServiceHost除了上面说的完全使用代码的方式,更好的方式是使用配置文件,把一些可能需要修改的属性跟代码分离,放到配置文件中,这样可以提供服务配置的灵活性,也更容易维护. 看看前面 ...

  6. WCF系列教程之WCF客户端调用服务

    1.创建WCF客户端应用程序需要执行下列步骤 (1).获取服务终结点的服务协定.绑定以及地址信息 (2).使用该信息创建WCF客户端 (3).调用操作 (4).关闭WCF客户端对象 二.操作实例 1. ...

  7. WCF系列(1)—— CustomBehavior 入门

    由于最近工作一直在做wcf平台上的开发,所以决定先结合自己平时工作中的经验实践写一个WCF的系列,希望能对大家有所帮助. 首先,说到WCF,就不得不提Endpoint这个概念,而Endpoint则由A ...

  8. 一步一个脚印学习WCF系列之WCF概要—WCF出现的目的(一)

    阅读目录 一:前言 二:WCF出现的目的是什么? 三:学了WCF能做什么? 四:为什么要把一个应用程序分布放在不同的计算机上? 一:前言 一个优秀的程序员,不能光凭一腔热血,不能只会写,却不知其所以然 ...

  9. WCF系列(三) -- WCF配置文件注释 【转】

    <?xml version="1.0" encoding="utf-8" ?> <configuration>     <!--  ...

最新文章

  1. 四种JOIN简单实例
  2. C 语言——字符串和格式化输入/输出
  3. 工业机器人焊钳制作_一种工业焊接机器人及其冷却装置的制作方法
  4. 对Android源码分析总结(Z)
  5. java中的string是什么_什么是String
  6. Oracle 导出部分表结构,以及导入
  7. iPhone SE 3共有三款:或将提供全面屏版本
  8. 事态升级是什么意思_为什么有的人越到关键时刻越容易掉链子?记住不要有“赌徒心理”...
  9. 6.这就是搜索引擎:核心技术详解 --- 链接分析
  10. Flutter之播放视频
  11. 计算机缩写术语完全介绍
  12. 合数阶群与素数阶群的双线性映射
  13. 把EditPlus添加到右键快捷菜单
  14. 摸鱼时间,画个吃豆人玩一下
  15. java 二进制最大值_java int型最大值/最小值,最大值+1,最小值-1
  16. 在Centos/Linux系统下使用Phalcon开发PHP项目
  17. HarmonyOS阶段测试(HarmonyOS应用程序框架揭秘)(4 )
  18. 机器人庄园作文_赛尔号想象作文
  19. 我如何从月薪1800到年薪百万到自由职业?
  20. 不使用函数实现字符串拼接函数strcat

热门文章

  1. MySQL中修改表结构的关键字_下列SQL语句中,修改表结构的关键字是
  2. cython python3_30倍!使用Cython加速Python代码
  3. linux数字雨代码解释,linux提权 漏洞合集 linux-kernel-exploits
  4. react不同环境不同配置angular_前端问题集:vue配置环境-给不同的环境配不同的打包命令...
  5. 指令系统——数据存放、指令寻址(详解)
  6. Chapter3-1_Speech Separation(Deep Clustering, PIT)
  7. LeetCode 314. 二叉树的垂直遍历(BFS/DFS)
  8. LeetCode 1151. 最少交换次数来组合所有的 1(滑动窗口)
  9. LeetCode 531. 孤独像素 I
  10. LintCode 633. 寻找重复的数(这个题要复习)