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();

            }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM