WCF-基本知識編程
(1)WCF服務元數據交換方式介紹:瀏覽器
WCF服務有兩種方案能夠發佈本身的元數據。一種是基於HTTP-GET協議提供元數據;另外一種則爲MEX終結點元數據交換方式,和WCF服務同樣使用一個專門
的終結點,稱爲MEX元數據交換終結點。
System.ServiceModel.Description命名空間裏MetadataExchangeClientMode枚舉類型裏進行了定義。代碼以下:網絡
using System; namespace System.ServiceModel.Description { // Summary: // Specifies the exchange mode used to obtain metadata. public enum MetadataExchangeClientMode { // Summary: // A WS-Transfer Get request is used. MetadataExchange = 0, // // Summary: // An HTTP GET request is used. HttpGet = 1, } }
元數據交換終結點與其它終結點類似,包含本身的地址(Address)、綁定(通訊協議Binding)、契約(服務、操做、數據Contract),可是使用的服務契約爲
WCF提供的接口IMetadataExchange。兩種發佈元數據的方式使用了兩種不一樣的標準網絡傳輸協議,前者爲HTTP/GET請求,後者爲WS-MetadataExchange(MEX:
WCF支持的基本綁定HTTP、HTTPS、TCP、IPC等綁定協議)。
啓用元數據交換服務後,必須顯式配置元數據交換行爲。下面咱們來分別詳細介紹WCF服務元數據交換配置和編程兩種方式的實現過程。tcp
(2)WCF服務元數據交換配置實現過程詳解:網絡傳輸協議
【2.1】配置HTTP-GET元數據交換方式:
須要配置服務的行爲和基地址,客戶端能夠根據基地址查看服務的元數據。代碼以下:spa
<service name="WcfServiceApp.WCFService" behaviorConfiguration="WcfServiceApp.WCFServiceBehavior"> <host> <baseAddresses> <add baseAddress="http://localhost:8001/"/> </baseAddresses> </host> </service> </services> <behaviors> <serviceBehaviors> <behavior name="WcfServiceApp.WCFServiceBehavior"> <!-- To avoid disclosing metadata information, set the value below to false and remove the metadat a endpoint above before deployment --> <serviceMetadata httpGetEnabled="true"/> <!-- To receive exception details in faults for debugging purposes, set the value below to true. Se t to false before deployment to avoid disclosing exception information --> <serviceDebug includeExceptionDetailInFaults="false"/> </behavior> </serviceBehaviors> </behaviors>
3 WCF服務元數據交換編程實現過程詳解:debug
【3.1】WCF服務元數據交換HTTP-GET編程實現:
必須添加對命名空間的引用, using System.ServiceModel.Description;咱們對服務元數據操做的類和接口信息定義在此命名空間裏,具體的實現HTTP-GET的
代碼以下:code
ServiceMetadataBehavior metadataBehavior;//定義服務行爲變量, metadataBehavior = host.Description.Behaviors.Find<ServiceMetadataBehavior>(); //獲取宿主的行爲列表 if (metadataBehavior == null)//若是沒有服務原數據交換的行爲,實例化添加服務原數據交換行爲 { metadataBehavior = new ServiceMetadataBehavior(); Uri httpAddress = new Uri("http://localhost:8001/"); metadataBehavior.HttpGetUrl =httpAddress; metadataBehavior.HttpGetEnabled = true;//設置HTTP方式 host.Description.Behaviors.Add(metadataBehavior); }
首先是得到服務行爲的列表信息,若是沒有設置,咱們就進行實例化服務原數據交換行爲,並設置http方式可
用。host.Description.Behaviors.Add(metadataBehavior);添加宿主服務的行爲。orm
在瀏覽器中查看以下圖:後綴爲wsdlblog
【3.2】WCF服務元數據交換WS-*編程實現:
這裏分別實現了HTTP、TCP、IPC三種方式的的元數據交換的代碼。和http-get方式略有不一樣,咱們須要實例化本身綁定元素和綁定,最後做爲參數傳遞給host
宿主實例。具體實現代碼以下:
//2編程方式實現ws*原數據交換
//生命三個綁定節點類 BindingElement tcpBindingElement = new TcpTransportBindingElement(); BindingElement httpBindingElement = new HttpsTransportBindingElement(); BindingElement pipeBindingElement = new NamedPipeTransportBindingElement(); //實例化通用綁定類的實例 Binding tcpBinding = new CustomBinding(tcpBindingElement); Binding httpBinding = new CustomBinding(httpBindingElement); Binding pipeBinding = new CustomBinding(pipeBindingElement); // Uri tcpBaseAddress = new Uri("net.tcp://localhost:9001/"); Uri httpBaseAddress = new Uri("http://localhost:9002/"); Uri pipeBaseAddress = new Uri("net.pipe://localhost/"); host.AddServiceEndpoint(typeof(WCFService.IWCFService), new NetTcpBinding(), tcpBaseAddress); host.AddServiceEndpoint(typeof(WCFService.IWCFService), new WSHttpBinding(), httpBaseAddress); host.AddServiceEndpoint(typeof(WCFService.IWCFService), new NetNamedPipeBinding(), pipeBaseAdd ress); //ServiceMetadataBehavior metadataBehavior;//定義服務行爲變量, metadataBehavior = host.Description.Behaviors.Find<ServiceMetadataBehavior>(); //獲取宿主的行爲列表 if (metadataBehavior == null)//若是沒有服務原數據交換的行爲,實例化添加服務原數據交換行爲 { metadataBehavior = new ServiceMetadataBehavior(); host.Description.Behaviors.Add(metadataBehavior); } //若是沒有可用的mex節點,能夠使用一下代碼判斷,添加mex節點 host.AddServiceEndpoint(typeof(IMetadataExchange), tcpBinding, "mex"); host.AddServiceEndpoint(typeof(IMetadataExchange), httpBinding, "mex"); host.AddServiceEndpoint(typeof(IMetadataExchange), pipeBinding, "mex");
在瀏覽器中查看以下圖:後綴變爲mex.