65.9K
CodeProject 正在变化。 阅读更多。
Home

BasicHttpBinding 与 WsHttpBinding 之间的区别

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.83/5 (79投票s)

2009 年 5 月 13 日

CPOL

5分钟阅读

viewsIcon

701618

downloadIcon

4962

BasicHttpBinding 与 WsHttpBinding 的区别。

目录

引言与目标

WCF 引入了许多绑定和协议。本文将重点介绍两个重要的协议:BasicHttpBinding WsHttpBinding ,它们看起来相似,但存在一些巨大的根本性差异。我们将先从区别开始,然后创建一个小型项目,实际观察这些区别。

我目前正在分发我 400 道问题与答案的电子书,涵盖了 WCF、WPF、WWF、AJAX、核心 .NET、SQL Server、架构等主要 .NET 相关主题。我相信您会喜欢这本电子书

先决条件

如果您是 WCF 新手,请阅读以下链接中的基础知识。WCF 的基础知识不在本文的讨论范围内。

BasicHttpBinding 与 WsHttpBinding 的区别

如果用一句话来概括 WsHttpBindingBasicHttpBinding 之间的区别,那就是 WsHttpBinding 支持 WS-* 规范。WS-* 规范就是扩展 Web 服务功能的标准。

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

标准 BasicHttpBinding WsHttpBinding
安全支持 这支持旧的 ASMX 风格,即 WS-BasicProfile 1.1。 它使用 WS-* 规范公开 Web 服务。
兼容性 它面向没有安装 .NET 3.0 的客户端,并支持更广泛的客户端。许多像 Windows 2000 这样的客户端仍然无法运行 .NET 3.0。因此,旧版本的 .NET 可以使用此服务。 由于它是使用 WS-* 规范构建的,因此它不支持更广泛的客户端,也不能被 3.0 以下版本的 .NET 使用。
SOAP 版本 SOAP 1.1 SOAP 1.2 和 WS-Addressing 规范。
可靠的消息传递 不支持。换句话说,如果客户端发送两到三次调用,您真的不知道它们是否会按相同顺序返回。 支持,因为它支持 WS-* 规范。
默认安全选项 默认情况下,当客户端调用发生时,消息没有提供安全性。换句话说,数据以明文形式发送。 由于 WsHttpBinding 支持 WS-*,因此默认启用了 WS-Security。所以数据不是以明文形式发送的。
安全选项
  • Windows – 默认身份验证
  • Basic
  • 证书
  • 传输
  • Message
  • 传输与消息凭据

您必须注意到的一个主要区别是安全性方面。默认情况下,BasicHttpBinding 以明文形式发送数据,而 WsHttpBinding 以加密和安全的方式发送数据。为了演示这一点,让我们创建两个服务,一个使用 BasicHttpBinding,另一个使用 WsHttpBinding,然后更详细地查看安全性方面。

我们将创建一个小型示例,以了解 BasicHttpBinding 如何以纯文本格式发送数据,以及 WsHttpBinding 如何加密数据。

注意:默认情况下,为实现互操作性,BasicHttpBinding 未启用安全性。换句话说,它就像我们旧的 Web 服务 ASMX。但这并不意味着我们不能在 BasicHttpBinding 中启用安全性。不久前,我写过一篇关于如何在 BasicHttpBinding 中启用安全性的文章

了解 BasicHttpBinding 与 WsHttpBinding 实际区别的五个步骤

为了理解这些实体之间的真正区别,我们将做一个小型项目。在这个项目中,我们将创建两个 WCF 服务,一个使用 BasicHttpBinding,第二个使用 WsHttpBinding

步骤 1:首先,让我们创建一个简单的使用 BasicHttpBinding 的服务。为此,我们只需创建一个简单的 WCF 项目,然后修改 ServiceModel 元素,如下所示。您可以在 endpoint 标签中看到我们指定了 basicHttpBinding 作为协议。

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

步骤 2:我们还需要创建另一个使用 WsHttpBinding 的服务。为此,您实际上不需要做任何特殊的事情。默认情况下,WCF 项目是使用 WsHttpBinding 创建的。下面是 Web.config 文件的样子。您可以看到 endpoint 标签如何使用 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>

步骤 3:我们不会在这两个服务中创建任何新方法。我们将只使用 WCF 模板创建的默认代码。因此,这两个服务都将有一个返回 stringGetData 函数。GetData 函数是 WCF 项目创建的默认函数。

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

步骤 4:现在我们的服务已经创建好了,我们需要创建一个客户端来消费这个服务。所以我们创建了一个简单的 Web 应用程序,并添加了两个引用,一个是服务引用,即 WsHttpBinding,第二个是 Web 引用,即 BasicHttpBinding。在这个例子中,我使用 Web 引用来添加 BasicHttpBinding,但您也可以通过使用“添加服务引用”来实现。我使用“添加 Web 引用”只是为了证明 BasicHttpBinding 实际上向后兼容旧的 Web 服务。

我们将在默认的 ASPX 页面上添加两个按钮。一个按钮将调用 HTTP 服务,另一个按钮将调用 WsHttp 服务。下面是在两个按钮点击事件中如何调用 GetData 函数。

步骤 5:现在我们的项目已经准备好了,是时候嗅探并查看数据在这两种情况下是如何在客户端和服务之间传输的了。让我们从这里下载一个简单的 HTTP 数据记录器。然后我们将一个接一个地单击这两个按钮,并使用 httpanalyzer 记录数据传输。对于基本 HTTP 协议,您可以看到发布的数据是简单的纯 XML 格式,而对于 wshttp 协议,它是加密格式。

换句话说,尽可能避免使用 BasicHttp

何时使用 BasicHttp 和 WsHttp 的考量

如果您需要向后兼容性并支持大量客户端,那么 basic HTTP binding 是不错的选择;否则,如果您看到客户端是 .NET 3.0 及以上版本,WsHttp 是一个很好的起点。

历史

  • 2009 年 5 月 13 日:首次发布。

如需进一步阅读,请观看以下面试准备视频和分步视频系列。

© . All rights reserved.