WCF中常用的binding方式

WCF中常用的binding方式:

BasicHttpBinding: 用于把 WCF 服务当作 ASMX Web 服务。用于兼容旧的Web ASMX 服务。
WSHttpBinding: 比 BasicHttpBinding 更加安全,通常用于 non-duplex 服务通讯。
WSDualHttpBinding: 和 WSHttpBinding 相比,它支持 duplex 类型的服务。
WSFederationHttpBinding: WS-Federation 安全通讯协议。
NetTcpBinding: 使用 TCP 协议,用于在局域网(Intranet)内跨机器通信。有几个特点:可靠性、事务支持和安全,优化了 WCF 到 WCF 的通信。限制是服务端和客户端都必须使用 WCF 来实现。
NetNamedPipeBinding: 使用命名管道进行安全、可靠、高效的单机服务通讯方式。
NetMsmqBinding: 使用消息队列在不同机器间进行非连接通讯。
NetPeerTcpBinding: 使用 P2P 协议在多机器间通讯。
MsmqIntegrationBinding: 将 WCF 消息转化为 MSMQ 消息,使用现有的消息队列系统进行跨机器通讯。如 MSMQ。

名称

传输

编码

共同操作

BasicHttpBinding

HTTP/HTTPS

Text

Yes

NetTcpBinding

TCP

Binary

No

NetPeerTcpBinding

P2P

Binary

No

NetNamedPipeBinding

IPC

Binary

No

WSHttpBinding

HTTP/HTTPS

Text,MTOM

Yes

WSFederationBinding

HTTP/HTTPS

Text,MTOM

Yes

WSDualHttpBinding

HTTP

Text,MTOM

Yes

NetMsmqBinding

MSMQ

Binary

No

MsmqIntegrationBinding

MSMQ

Binary

Yes

Binding名称

Configuration Element

描述

BasicHttpBinding

basicHttpBinding

一个指定用符合基本网络服务规范通讯的binding,它用http进行传输,数据格式为text/xml

WSHttpBinding

wsHttpBinding

一个安全的通用的binding,但它不能在deplex中使用

WSDualHttpBinding

wsDualHttpBinding

一个安全的通用的binding,但能在deplex中使用

WSFederationHttpBinding

wsFederationHttpBinding

一个安全的通用的支持WSF的binding,能对用户进行验证和授权

NetTcpBinding

netTcpBinding

在wcf应用程序中最适合跨机器进行安全通讯的binding

NetNamedPipeBinding

netNamedPipeBinding

在wcf应用程序中最适合本机进行安全通讯的binding

NetMsmqBinding

netMsmqBinding

在wcf应用程序中最适合跨机器进行安全通讯的binding,并且支持排队

NetPeerTcpBinding

netPeerTcpBinding

一个支持安全的,多机交互的binding

MsmqIntegrationBinding

msmqIntegrationBinding

一个用于wcf与现有msmq程序进行安全通讯的binding

绑定类名称

传输

消息编码

消息版本

安全模式

可靠消息传送

事务流(默认情况下禁用)

BasicHttpBinding

HTTP

文本

SOAP 1.1

不支持

不支持

WSHttpBinding

HTTP

文本

SOAP 1.2 WS-Addressing 1.0

消息

禁用

WS-AtomicTransactions

WSDualHttpBinding

HTTP

文本

SOAP 1.2 WS-Addressing 1.0

消息

启用

WS-AtomicTransactions

WSFederationHttpBinding

HTTP

文本

SOAP 1.2 WS-Addressing 1.0

消息

禁用

WS-AtomicTransactions

NetTcpBinding

TCP

二进制

SOAP 1.2

传输

禁用

OleTransactions

NetPeerTcpBinding

P2P

二进制

SOAP 1.2

传输

不支持

不支持

NetNamedPipesBinding

命名管道

二进制

SOAP 1.2

传输

不支持

OleTransactions

NetMsmqBinding

MSMQ

二进制

SOAP 1.2

消息

不支持

不支持

MsmqIntegrationBinding

MSMQ

不支持(使用 WCF 之前的序列化格式)

不支持

传输

不支持

不支持

CustomBinding

您决定

您决定

您决定

您决定

您决定

您决定

可以通过修改WCF配置文件来修改binding的方式,修改WCF配置文件可以使用VS自带的WCF配置修改工具(WCF Service Configuration Editor)

这里写一个配置文件的注释,这份是一个Service端的配置文件(不是Host,所以能公开元数据)。

<!-- Service端的配置文件,公开元数据与Http连接 -->

    <system.serviceModel>                                     WCF 配置开始

        <behaviors>                                           一个服务器行为的配置开始

            <serviceBehaviors>                                

                <behaviorname="NewBehavior">                 配置一个行为 name:行为的名称

                    <serviceDebug />                          表示此service可以调试     

                    <serviceMetadatahttpGetEnabled="true"httpsGetEnabled="false" />

                    元数据的配置, httpGetEnabled:是否可以使用Http来获取元数据,

httpsGetEnabled:是否可以使用Https来获取元数据

                    当设置为可以获取元数据, 就必须配置http或https的路径

                </behavior>

            </serviceBehaviors>

        </behaviors>                                          服务器行为配置结束

       

        <services>                                            服务器配置开始      

          <service behaviorConfiguration="NewBehavior"       配置一个服务器, behaviorConfiguration:指示服务行为,b连接到behaviors

                     name="Service">   name:服务类的位置

            <endpoint                                         开始一个端口的配置

                   binding="basicHttpBinding"                 binding: 绑定类型 Http

                   contract=" IShopServiceV2" >

                                                              contract:公共接口的位置

            </endpoint>

            <endpoint                         开始第二个端口的配置,这个端口是用于发布元数据

                    address="mex"             address:端口的监听位置,mex表示设置成相对地址

                    binding="mexHttpBinding"                  binding: 绑定方式.

                                                             此处为元数据的绑定方式

                                                     mexHttpBinding(对于 HTTP 发布)。

                                                     mexHttpsBinding(对于 HTTPS 发布)。

                                                     mexNamedPipeBinding(对于命名管道发布)。

                                                     mexTcpBinding(对于 TCP 发布)。

                    contract="IMetadataExchange"/>            contract:公共接口的位置

                                      IMetadataExchange: 公开用于返回有关服务的元数据的方法。

            <host>                                            主机配置

                <baseAddresses>                               

                    <addbaseAddress="http:127.0.0.1:8001/WCFService" />

                                                              配置一个服务器的监听路径

                </baseAddresses>

            </host>

          </service>

        </services>

代理类生成:不单是WCF服务文件(.svn)能用工具(svcutil.exe)来生成客户端代理,即使使用WCF服务库也可以通过WCF服务主机的元数据地址来使用svcutil.exe生成代理。

一个双绑定,带配置文件的Host的Demo:

 <system.serviceModel>

    <services>

      <servicename="Service">

        <endpointbinding="netTcpBinding"contract="IShopServiceV2" />

        <endpointbinding="basicHttpBinding"contract="IShopServiceV2" />

      </service>

    </services>

 </system.serviceModel>

                Uri tcpAddress = new Uri("net.tcp://localhost:9000/Service");

                Uri httpAddress = new Uri("http://localhost:9001/Service");

                ServiceHost Host = new ServiceHost(typeof(WCFCompnent.Service), tcpAddress, httpAddress);

                Host.Open();

                Console.WriteLine("服务已经启动!");

                Console.Read();

                Host.Close();

不带配置文件的Host:

            Uri baseAddress = new Uri("net.tcp://localhost:9000/ServoceHost");

            Uri mexAddress = new Uri("mex", UriKind.Relative);

            using (ServiceHost serviceHost = new ServiceHost(typeof(Service), baseAddress))

            {

                NetTcpBinding binding = new NetTcpBinding();

                serviceHost.AddServiceEndpoint(typeof(PublicElements.publicInterface.IService), binding, baseAddress);

                serviceHost.Open();

                Console.WriteLine("服务器已经打开");

                Console.WriteLine("按任意键关闭");

                Console.Read();

                serviceHost.Close();

            }

posted on 2015-02-28 15:24  老有所依  阅读(6203)  评论(0编辑  收藏  举报

导航