自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

關(guān)于WCF服務(wù)元數(shù)據(jù)交換編程揭密

開(kāi)發(fā) 后端
這里實(shí)現(xiàn)了WCF服務(wù)元數(shù)據(jù)交換HTTP-GET編程實(shí)現(xiàn)、WCF服務(wù)元數(shù)據(jù)交換WS-*編程實(shí)現(xiàn),文章有詳細(xì)的代碼和介紹,希望大家看了會(huì)有幫助。

WCF還是比較常用的,于是我研究了一下WCF服務(wù)元數(shù)據(jù)交換,在這里拿出來(lái)和大家分享一下,希望對(duì)大家有用。前者配置簡(jiǎn)單、快捷,后者相對(duì)復(fù)雜。但是編程方式允許代碼運(yùn)行時(shí)控制或者設(shè)置元數(shù)據(jù)交換的信息。因而更加靈活。下面我們就來(lái)看看如何通過(guò)代碼實(shí)現(xiàn)剛才的服務(wù)原數(shù)據(jù)交換的配置。

WCF服務(wù)元數(shù)據(jù)交換HTTP-GET編程實(shí)現(xiàn):

必須添加對(duì)命名空間的引用, using System.ServiceModel.Description;我們對(duì)服務(wù)元數(shù)據(jù)操作的類(lèi)和接口信息定義在此命名空間里,具體的實(shí)現(xiàn)HTTP-GET的代碼如下:

  1. ServiceMetadataBehavior metadataBehavior;  
  2. //定義服務(wù)行為變量,  
  3. metadataBehavior = host.Description.Behaviors.Find<ServiceMetadataBehavior>();  
  4. //獲取宿主的行為列表  
  5. if (metadataBehavior == null)  
  6. //如果沒(méi)有服務(wù)原數(shù)據(jù)交換的行為,實(shí)例化添加服務(wù)原數(shù)據(jù)交換行為  
  7. {  
  8. metadataBehavior = new ServiceMetadataBehavior();  
  9. Uri httpAddress = new Uri("http://localhost:8001/");  
  10. metadataBehavior.HttpGetUrl =httpAddress;  
  11. metadataBehavior.HttpGetEnabled = true;//設(shè)置HTTP方式  
  12. host.Description.Behaviors.Add(metadataBehavior);  

#T#首先是獲得服務(wù)行為的列表信息,如果沒(méi)有設(shè)置,我們就進(jìn)行實(shí)例化服務(wù)原數(shù)據(jù)交換行為,并設(shè)置http方式可用。 host.Description.Behaviors.Add(metadataBehavior);添加宿主服務(wù)的行為。

WCF服務(wù)元數(shù)據(jù)交換WS-*編程實(shí)現(xiàn):

這里分別實(shí)現(xiàn)了HTTP、TCP、IPC三種方式的的元數(shù)據(jù)交換的代碼。和http-get方式略有不同,我們需要實(shí)例化自己綁定元素和綁定,***作為參數(shù)傳遞給host宿主實(shí)例。具體實(shí)現(xiàn)代碼如下:

  1. //2編程方式實(shí)現(xiàn)ws*原數(shù)據(jù)交換  
  2. //生命三個(gè)綁定節(jié)點(diǎn)類(lèi)  
  3. BindingElement tcpBindingElement = new TcpTransportBindingElement();  
  4. BindingElement httpBindingElement = new HttpsTransportBindingElement();  
  5. BindingElement pipeBindingElement = new NamedPipeTransportBindingElement();  
  6. //實(shí)例化通用綁定類(lèi)的實(shí)例  
  7. Binding tcpBinding = new CustomBinding(tcpBindingElement);  
  8. Binding httpBinding = new CustomBinding(httpBindingElement);  
  9. Binding pipeBinding = new CustomBinding(pipeBindingElement);  
  10. //  
  11. Uri tcpBaseAddress = new Uri("net.tcp://localhost:9001/");  
  12. Uri httpBaseAddress = new Uri("http://localhost:9002/");  
  13. Uri pipeBaseAddress = new Uri("net.pipe://localhost/");  
  14. host.AddServiceEndpoint(typeof(WCFService.IWCFService), new NetTcpBinding(), tcpBaseAddress);  
  15. host.AddServiceEndpoint(typeof(WCFService.IWCFService), new WSHttpBinding(), httpBaseAddress);  
  16. host.AddServiceEndpoint(typeof(WCFService.IWCFService), new NetNamedPipeBinding(), pipeBaseAddress);  
  17.  
  18. //ServiceMetadataBehavior metadataBehavior;//定義服務(wù)行為變量,  
  19. metadataBehavior = host.Description.Behaviors.Find<ServiceMetadataBehavior>();  
  20. //獲取宿主的行為列表  
  21. if (metadataBehavior == null)//如果沒(méi)有服務(wù)原數(shù)據(jù)交換的行為,實(shí)例化添加服務(wù)原數(shù)據(jù)交換行為  
  22. {  
  23. metadataBehavior = new ServiceMetadataBehavior();  
  24.  
  25. host.Description.Behaviors.Add(metadataBehavior);  
  26. }  
  27. //如果沒(méi)有可用的mex節(jié)點(diǎn),可以使用一下代碼判斷,添加mex節(jié)點(diǎn)  
  28.  
  29. host.AddServiceEndpoint(typeof(IMetadataExchange), tcpBinding, "mex");  
  30. host.AddServiceEndpoint(typeof(IMetadataExchange), httpBinding, "mex");  
  31. host.AddServiceEndpoint(typeof(IMetadataExchange), pipeBinding, "mex");  


 

責(zé)任編輯:田樹(shù) 來(lái)源: 博客
相關(guān)推薦

2010-03-02 10:50:57

WCF元數(shù)據(jù)交換

2009-11-06 10:25:34

WCF元數(shù)據(jù)交換

2009-11-06 10:37:57

配置WCF服務(wù)

2009-11-09 17:17:31

WCF元數(shù)據(jù)交換

2009-12-22 16:14:01

WCF服務(wù)元數(shù)據(jù)

2009-11-09 16:14:49

WCF服務(wù)元數(shù)據(jù)

2009-11-06 10:01:07

WCF服務(wù)元數(shù)據(jù)

2009-12-21 16:37:41

WCF獲取服務(wù)元數(shù)據(jù)

2009-07-01 09:43:40

WCF安全元數(shù)據(jù)

2009-11-09 17:30:20

WCF元數(shù)據(jù)

2018-08-31 21:00:39

數(shù)據(jù)交換模型數(shù)據(jù)模型應(yīng)用程序

2010-01-15 10:19:42

數(shù)據(jù)交換技術(shù)

2019-11-22 08:40:19

ProtobufGo編程語(yǔ)言

2010-02-22 11:02:06

WCF元數(shù)據(jù)

2009-01-03 14:54:40

ibmdwXML

2011-08-25 16:53:42

Lua數(shù)據(jù) 交換

2012-05-08 09:10:56

WCF

2011-08-19 13:45:14

iPhone應(yīng)用iPhone OS數(shù)據(jù)

2009-11-05 16:21:51

WCF服務(wù)

2010-02-04 11:15:12

數(shù)據(jù)交換技術(shù)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)