WCF中幾個基本知識點整理
1、WCF中的Message
{
Created,
Read,
Written,
Copied,
Closed
}
對Message對象,只有在Create狀態下,才能讀,否則讀取會有異常。Message
同樣定義了許多Write之類的方法,這些Write方法同樣也只能用于處于Created的消息。
在WCF中,有可能需要對消息進行一些讀取、等操作。但是讀取操作會改變Message的
狀態,導致再次讀取會有異 常。這時,可以使用Message定義的CreateBufferedCopy方法。
它的簽名如下:
它返回的是MessageBuffer對象,它能進行多次Create而返回Message對象,并且Message的狀態為Created。
大數據量二進制數據以SOAP傳輸時優化使用的格式,也與平臺無關;Binary是以二進制格式編碼,
只用于.Net平臺上。
服務可以根據他發布自己的元數據。
是自動發布服務的元數據信息,于是我們可以通過WSDL工具生成WebService的代理類。
但是我們可以通過發布元數據交換終結點,生成服務的代理。
WCF自動為服務宿主自動提供了IMetadataExchange接口的實現。對于元數據交換終結點,
WCF提供了專門的綁定元素用以對不同的協議(如:HTTP、TCP、IPC)的支持。如HTTP則對應
mexHttpBinding;對于Tcp則對應mexTcpBinding;對于IPC則對應mexNamedPipeBinding。
<behaviors>
<serviceBehaviors>
<behavior name="mex">
<serviceMetadata/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="Service.CalculatorService" behaviorConfiguration="mex">
<host>
<baseAddresses>
<add baseAddress="net.tcp://127.0.0.1:3636/mexTcp" />
<add baseAddress="http://127.0.0.1:6363/mexHttp"/>
<add baseAddress="net.pipe://127.0.0.1"/>
</baseAddresses>
</host>
<endpoint address="net.tcp://localhost:3636/SessionfulCalculator" binding="netTcpBinding" contract="Contract.ICalculator"></endpoint>
<endpoint address="http://localhost:6363/SessionfulCalculator" binding="wsHttpBinding" contract="Contract.ICalculator"></endpoint>
<endpoint address="netpipe" binding="netNamedPipeBinding" contract="Contract.ICalculator"></endpoint>
<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"> </endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"> </endpoint>
<endpoint address="mex" binding="mexNamedPipeBinding" contract="IMetadataExchange"> </endpoint>
</service>
</services>
</system.serviceModel>
這樣,不管服務所支持的HTTP、TCP、IPC等方式,我們都可以通過SVCUtil工具生成代理類
進而訪問服務。
在WCF中,對于HTTP協議而言,我們可以通過直接配置服務的行為,通過httpGetEnabled="true"
的方式來發布服務的元數據信息,而它不支持其他協議;對于其他協議我們希望發布元數據信息,
通過配置的方式顯然是一種很好的方式。
contract="IMetadataExchange"></endpoint>
如果address配置為空或者與behavior中 name配置相同,則可以直接通過SVCUtil加上
baseAddress生成代理類。如下圖:
如果address和behavior中 name配置不同則生成代理類時SVCUtil 需要加上baseAddress +
<endpointaddress="mex" binding="mexNamedPipeBinding"contract="IMetadataExchange" />
中的address的屬性值。如下圖 :

<serviceMetadata httpGetEnabled="false"/>
</behavior>
浙公網安備 33010602011771號