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

WCF尋址報頭經(jīng)驗之談

開發(fā) 開發(fā)工具
WCF尋址報頭可以幫助我們來解決一些比較復雜的問題,在這篇文章中,我們會為大家詳細介紹一下這方面的具體應用技巧。

WCF中的地址獲取在實際編程中是一個比較重要的操作技術。我們今天為大家介紹的是有關這方面的一些基礎知識,比如有關WCF尋址報頭的一些應用技巧。希望可以給大家?guī)硪恍椭?/p>

在某些情況下,我們可能希望通過自定義尋址報頭來解決一些復雜的問題,如根據(jù)根據(jù)傳入的WCF尋址報頭中是否包含某些信息,將其轉(zhuǎn)發(fā)到不同的終結點,通過自定義尋址報頭,可以實現(xiàn)SOAP消息的無限擴展,放置任何希望的控制信息到SOAP消息。如下面的代碼:

  1. using (ServiceHost calculatorServiceHost =  
  2. new ServiceHost(typeof(CalculatorService),  
  3. new Uri("http://localhost:8887/CalculatorService")))  
  4. {  
  5. calculatorServiceHost.Opened += delegate  
  6. {  
  7. Console.WriteLine("Service begin to listen via the Address:{0}",  
  8. calculatorServiceHost.BaseAddresses[0].ToString());  
  9. };  
  10. AddressHeader header =  
  11. AddressHeader.CreateAddressHeader("basic",  
  12. "http://www.cnblogs.com/terrylee", "Terrylee");  
  13. EndpointAddress ea = new EndpointAddress(  
  14. new Uri("http://localhost:8887/CalculatorService"), header);  
  15. calculatorServiceHost.Description.Endpoints.Add(  
  16. new ServiceEndpoint(  
  17. ContractDescription.GetContract(typeof(ICalculator)),  
  18. new WSHttpBinding(),  
  19. ea));  
  20. ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();  
  21. behavior.HttpGetEnabled = true;  
  22. calculatorServiceHost.Description.Behaviors.Add(behavior);  
  23. calculatorServiceHost.Open();  
  24. Console.Read();  

我們在WSDL中可以看到該自定義的報頭,它作為終結點引用的引用參數(shù):

  1. < wsdl:service name="CalculatorService"> 
  2. < wsdl:port name="WSHttpBinding_ICalculator" 
    binding="tns:WSHttpBinding_ICalculator"> 
  3. < soap12:address location="http://localhost:8887/CalculatorService" /> 
  4. < wsa10:EndpointReference> 
  5. < wsa10:Address>http://localhost:8887/CalculatorService
    < /wsa10:Address> 
  6. < wsa10:ReferenceParameters> 
  7. < basic xmlns="http://www.cnblogs.com/terrylee">Terrylee< /basic> 
  8. < /wsa10:ReferenceParameters> 
  9. < Identity xmlns="http://schemas.xmlsoap.org/ws/2006/02/
    addressingidentity"
    > 
  10. < Upn>TerryLee-PC\TerryLee< /Upn> 
  11. < /Identity> 
  12. < /wsa10:EndpointReference> 
  13. < /wsdl:port> 
  14. < /wsdl:service> 

截獲到SOAP消息可以看到,在消息報頭中添加了basic這樣的信息,如下代碼所示:

  1. < s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> 
  2. < s:Header> 
  3. < basic xmlns="http://www.cnblogs.com/terrylee">Terrylee< /basic> 
  4. < To s:mustUnderstand="1">http://localhost:8887/CalculatorService< /To> 
  5. < Action s:mustUnderstand="1">http://tempuri.org/ICalculator/Add< /Action> 
  6. < /s:Header> 
  7. < s:Body> 
  8. < Add xmlns="http://tempuri.org/"> 
  9. < x>1< /x> 
  10. < y>2< /y> 
  11. < /Add> 
  12. < /s:Body> 
  13. < /s:Envelope> 

當然我們也可以通過配置的方式對WCF尋址進行自定義WCF尋址報頭,如下代碼所示:

  1. < service name="TerryLee.WCFAddressing.Service.CalculatorService" 
  2. behaviorConfiguration="calculatorBehavior"> 
  3. < host> 
  4. < baseAddresses> 
  5. < add baseAddress="http://localhost:8887/Calculator"/> 
  6. < /baseAddresses> 
  7. < /host> 
  8. < endpoint address="" 
  9. binding ="wsHttpBinding" 
  10. contract="TerryLee.WCFAddressing.Contract.ICalculator"> 
  11. < headers> 
  12. < basic xmlns="http://www.cnblogs.com/terrylee">Terrylee< /basic> 
  13. < /headers> 
  14. < /endpoint> 
  15. < /service> 

WCF尋址報頭的一些基本內(nèi)容就為大家介紹到這里。

【編輯推薦】

  1. WCF框架處理流程基本功能解析
  2. WCF服務加載實際應用方法詳解
  3. AJAX WCF服務項模板正確使用方法介紹
  4. WCF返回值適用場景分析
  5. WCF數(shù)據(jù)量在實際應用中錯誤解決方法
責任編輯:曹凱 來源: 博客園
相關推薦

2009-11-09 17:06:38

WCF選擇綁定

2009-11-05 15:50:25

WCF behavio

2009-12-23 10:07:57

學習WPF

2009-12-17 18:12:31

Ruby框架應用

2009-12-09 16:58:03

PHP strtoti

2010-04-30 14:31:58

Unix系統(tǒng)

2010-04-22 18:07:28

Aix系統(tǒng)維護

2009-11-02 15:16:07

VB.NET編程

2012-05-23 09:43:59

Linux升級經(jīng)驗

2009-11-16 15:32:05

PHP數(shù)組函數(shù)

2010-07-26 15:36:59

CCNP

2009-06-16 08:49:51

2009-12-08 11:44:14

PHP獲取隨機數(shù)

2010-04-29 12:09:42

Unix服務器

2012-07-06 15:31:29

Linux系統(tǒng)

2020-01-10 18:25:00

Go語言JavaScript編程語言

2009-11-03 10:40:57

VB.NET Prot

2009-10-28 17:00:30

VB.NET數(shù)據(jù)庫

2010-01-15 10:48:29

C++程序代碼

2010-08-09 14:28:04

職業(yè)生涯
點贊
收藏

51CTO技術棧公眾號