運作WCF服務
這裡通過自宿主方式self-host來運作wcf服務。
公開終結點Endpoint,終結點由ServiceEndpoint 類來實作。它有很多的成員。其中要用到的是所說的ABC。
Address,Binding,Contract,位址,綁定,契約。
(一)Address
其中的Address,由EndpointAddress 來實作,它有很多成員:
·Uri 用于擷取終結點的位址
·Identity 擷取用于驗證終結點的辨別
·Headers 用來建立終結點位址标頭集合
除了這幾個還有其它一些屬性:
ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.chs/fxref_system.servicemodel
/html/8947f21b-7f57-fb56-512e-25554808d870.htm
位址是用來公開終結點位址的,這個位址的配置可以由程式設計式實作,或由xml配置檔案配置實作。位址可以分為相對位址和絕對位址。下邊說明一下兩種實作方式:
(1)程式設計方式實作
Uri _baseAddress = new Uri("http://localhost:8731/FirstService");
ServiceHost host = new ServiceHost(typeof(CaculateService), _baseAddress);
EndpointAddress address = new EndpointAddress(_baseAddress);
host.Description.Endpoints[0].Address = address;
這裡用一下Headers屬性:
ServiceHost host = new ServiceHost(typeof(CaculateService));
AddressHeader header1 = AddressHeader.CreateAddressHeader
("header_xxx", "namespace_xxx", "value_xxx");
AddressHeader header2 = AddressHeader.CreateAddressHeader
("header_yyy", "namespace_yyy", "value_yyy");
AddressHeader[] headers=new AddressHeader[]{header1,header2};
EndpointAddress address = new EndpointAddress(_baseAddress,headers);
下邊我截取soap頭部分内容,可以看到:
<header_xxx a:IsReferenceParameter="true" xmlns="namespace_xxx">
value_xxx
</header_xxx>
<header_yyy a:IsReferenceParameter="true" xmlns="namespace_yyy">
value_yyy
</header_yyy>
我們在soap頭中添加了一些頭資訊。然後,在用戶端:
foreach (AddressHeader header in client.Endpoint.Address.Headers)
{
Console.WriteLine(
header.Name + " " +
header.Namespace + " " +
header.GetValue<string>());
}
輸出:
header_xxx namespace_xxx value_xxx
header_yyy namespace_yyy value_yyy
(2)Xml配置檔案
現在在app.config檔案中進行配置,配置基位址,同時配置中繼資料釋出,及soap頭兩條資訊:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="FirstService.CaculateServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="FirstService.CaculateServiceBehavior"
name="FirstService.CaculateService">
<endpoint
address=""
binding="wsHttpBinding"
contract="FirstService.ICaculateService">
<headers>
<header_xxx>self_xxx</header_xxx>
<header_yyy>self_yyy</header_yyy>
</headers>
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<host>
<baseAddresses>
<add baseAddress=
"http://localhost:8731/FirstService/CaculateService/" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
</configuration>
在這裡的配置與程式設計方式是一樣的。如果不熟悉這種配置方式,可以通過WCF服務配置編輯器來進行。
位置:工具-》WCF服務配置編輯器
在用戶端得到的結果為:
header_xxx self_xxx
header_yyy self_yyy
因為沒有為兩個頭指定名字空間,是以為空。
位址指定了終結點的位置,讓用戶端可以找到終結點,而終結點公開了服務。
(二)Binding
綁定,提供了通信的細節:
·傳輸,傳輸協定用Http,還是Tcp,或是IPC等等
·消息編碼格式,這裡的編碼格式是指:二進制,文本,MTOM
·安全
系統提供了一些綁定。這裡列舉幾個:
綁定 | 配置元素 | 說明 |
BasicHttpBinding | <basicHttpBinding> | 一個綁定,适用于與符合WS-BasicProfile的Web服務(例如,基于ASP.NETWeb服務(ASMX)的服務)進行的通信。此綁定将HTTP用作傳輸協定,并将文本/XML用作預設消息編碼。 |
WSHttpBinding | <wsHttpBinding> | 一個安全的、可互操作的綁定,适合于非雙工服務協定。 |
WSDualHttpBinding | <wsDualHttpBinding> | 一個安全且可互操作的綁定,适用于雙工服務協定或通過SOAP媒介進行的通信。 |
NetTcpBinding | <netTcpBinding> | 一個安全且經過優化的綁定,适用于WCF應用程式之間跨計算機的通信。 |
NetNamedPipeBinding | <netNamedPipeBinding> | 一個安全、可靠且經過優化的綁定,适用于WCF應用程式之間計算機上的通信。 |
NetMsmqBinding | <netMsmqBinding> | 一個排隊綁定,适用于WCF應用程式之間的跨計算機的通信。 |
NetPeerTcpBinding | <netPeerTcpBinding> | 一個啟用安全的多計算機通信的綁定。 |
MsmqIntegrationBinding | <msmqIntegrationBinding> | 一個适合于WCF應用程式和現有消息隊列應用程式之間的跨計算機通信的綁定。 |
ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.chs/
wcf_con/html/2c243746-45ce-4588-995e-c17126a579a6.htm
這裡說一下:BindingElement類型:用于為各種類型的通道生成通道工廠和通道偵聽器以處理傳出和傳入消息的綁定元素。這是一個抽象基類,而綁定是由一組繼承自抽象基類BindingElement的有序元素組成。
這裡列舉一下:
OneWayBindingElement
TransportBindingElement
CompositeDuplexBindingElement;
StreamUpgradeBindingElement
PrivacyNoticeBindingElement
UseManagedPresentationBindingElement
ReliableSessionBindingElement
SecurityBindingElement
MessageEncodingBindingElement
PeerResolverBindingElement
TransactionFlowBindingElement
ContextBindingElement
這裡再說明一些:
(1)編碼MessageEncodingBindingElement
BinaryMessageEncodingBindingElement | 用于指定編碼消息時所采用的.NET二進制XML格式的綁定元素 |
MtomMessageEncodingBindingElement | 指定消息傳輸優化機制(MTOM)消息所使用的編碼和版本管理的綁定元素 |
TextMessageEncodingBindingElement | 綁定元素,指定用于基于文本的SOAP消息的字元編碼與消息版本管理 |
WebMessageEncodingBindingElement | 在WindowsCommunicationFoundation(WCF)綁定中使用純文字XML與JavaScript對象表示法(JSON)消息編碼以及“原始”二進制内容時,能夠對其進行讀寫 |
(2)傳輸TransportBindingElement
(3)事務TransactionFlowBindingElement
(4)安全SecurityBindingElement
(5)可靠性 ReliableSessionBindingElement
下邊提供一個清單。
傳輸 | 編碼 | 互操作 | |
Http/Https | Text,MTOM | Yes | |
Tcp | binary | No | |
P2p | Binary | ||
IPC | |||
WSFederationHttpBinding | |||
Http | |||
MSMQ | |||
在例子中,用到的綁定是:wsHttpBinding
它支援Http/Https傳輸,且編碼支援文本和MTOM
這裡示範一下兩種編碼格式對soap對象是怎麼樣一種表現。
它預設的編碼是文本,就是常見的那種,就一個soap,一個soap封套。而如果是MTOM呢,就會以一種soap封套加soap附件格式傳輸
在配置檔案中可以這樣設定:
第一部分,這是終結點的ABC,其中綁定要求綁定細節,即粗體部分
<endpoint address="" binding="wsHttpBinding" bindingConfiguration="BindingSelf" contract="FirstService.ICaculateService">
第二部分,用于配置綁定細節
<bindings>
<binding name="BindingSelf" messageEncoding="Mtom">
</binding>
</wsHttpBinding>
</bindings>
這裡有個messageEncoding,因為用的是wsHttpBinding,而它支援兩種編碼,是以這裡隻有兩個枚舉值:mtom和text
我這裡提供一段MTOM消息編碼流:
--uuid:55e877da-b9b1-4091-9832-f1dd79ece1d4+id=2
Content-ID: <http://tempuri.org/0>
Content-Transfer-Encoding: 8bit
Content-Type: application/xop+xml;charset=utf-8;type="application/soap+xml"
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">
省略……
</s:Envelope>
--uuid:55e877da-b9b1-4091-9832-f1dd79ece1d4+id=2--
這與Text編碼不同。Text編碼隻一個封套。
部落格園大道至簡
http://www.cnblogs.com/jams742003/轉載請注明:部落格園