1. 程式人生 > >WCF系列(一)BasicHttpBinding 和 WsHttpBinding 的不同點

WCF系列(一)BasicHttpBinding 和 WsHttpBinding 的不同點

client inspect pat ges exceptio host ddr 小項目 infer

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。

WCF系列(一)BasicHttpBinding 和 WsHttpBinding 的不同點