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

WCF使用Header如何正確實(shí)現(xiàn)

開發(fā) 開發(fā)工具
我們?nèi)绻朐赪CF中使用自定義的Header的話,應(yīng)該如何進(jìn)行正確的操作呢?在這里大家就可以充分的掌握到WCF使用Header的相關(guān)技巧。

對(duì)于一個(gè)經(jīng)驗(yàn)豐富的編程人員來(lái)說(shuō),它不可能不知道WCF為何物。作為一個(gè).NET Framework 3.5的重要組成部件,為我們帶來(lái)了非常大的好處。我們?cè)谶@里先來(lái)了解一下WCF使用Header的相關(guān)應(yīng)用技巧。

在WCF中如何實(shí)現(xiàn)登陸,典型的場(chǎng)景如下:

 

 

 

  1. [ServiceContract]  
  2. public interface ILogin {  
  3. [OperationContract]  
  4. bool Signin(string userName, string password);  
  5. }  
  6. [ServiceContract]  
  7. public interface IBizTest {  
  8. [OperationContract]  
  9. string GetWelcomeInfo();  

 

千萬(wàn)別從WCF自帶的那個(gè)InstanceContextMode來(lái)想辦法,因?yàn)閃CF中的PerSession調(diào)用只是針對(duì)每個(gè)服務(wù)類而言的,除非你變態(tài)到服務(wù)端只有一個(gè)類來(lái)實(shí)現(xiàn)全部的接口;#t#

變個(gè)思路,能不能用類似.NET Remoting中的CallContext呢?但是查了一下WCF的手冊(cè),好像也沒有這么個(gè)東西,怎么解決呢?那就是Custom header.

解決方案提出前,需要知道一點(diǎn)的就是,服務(wù)端取客戶端送出的Header的方法:

先遍歷OperationContext.Current.IncomingMessageHeaders找出客戶端發(fā)送的Header Name,然后再用 OperationContext.Current.IncomingMessageHeaders.GetHeader<T>(i)得到值就可以啦。

下面的問(wèn)題就剩下客戶端怎么發(fā)送Custom Header了。

策略1:在每個(gè)客戶端Proxy中增加類似如下的代碼

 

  1. using (OperationContextScope scope = new 
    OperationContextScope(InnerChannel)) {  
  2. MessageHeader mh = MessageHeader.CreateHeader("HeaderName", 
    string.Empty, "HeaderValue");  
  3. OperationContext.Current.OutgoingMessageHeaders.Add(mh);  
  4. //…  
  5. }  
  6.  

 

 

但是每個(gè)客戶端都要增加,這樣的WCF使用Header的步驟太麻煩了,所以,引出

2.自定義一個(gè)CallContextAttribute,代碼如下:

1. 先定義一個(gè)IClientMessageInspector接口的實(shí)現(xiàn)類

  1. public class ContextHeader : IClientMessageInspector {  
  2. public void AfterReceiveReply(ref System.ServiceModel.
    Channels.Message reply, object correlationState) {  
  3. //  
  4. }  
  5. public object BeforeSendRequest(ref System.ServiceModel.
    Channels.Message request, IClientChannel channel) {  
  6. MessageHeader clientHeader = MessageHeader.CreateHeader
    ("headerName", string.Empty, "headerValue");  
  7. request.Headers.Add(clientHeader);  
  8. return null;  
  9. }  

 

 

OK , 然后就可以實(shí)現(xiàn)CallContextAttribute了

  1. public class CallContextAttribute : Attribute, IEndpointBehavior,
     IOperationBehavior {  
  2. IEndpointBehavior Members#region IEndpointBehavior Members  
  3. public void AddBindingParameters(ServiceEndpoint endpoint, 
    BindingParameterCollection bindingParameters) {  
  4. }  
  5. public void ApplyClientBehavior(ServiceEndpoint endpoint, 
    ClientRuntime clientRuntime) {  
  6. clientRuntime.MessageInspectors.Add(new ContextHeader());  
  7. }  
  8. public void ApplyDispatchBehavior(ServiceEndpoint endpoint, 
    EndpointDispatcher endpointDispatcher) {  
  9. }  
  10. public void Validate(ServiceEndpoint endpoint) {  
  11. }  
  12. #endregion  
  13. IOperationBehavior Members#region IOperationBehavior Members  
  14. public void AddBindingParameters(OperationDescription operationDescription, 
    BindingParameterCollection bindingParameters) {  
  15. }  
  16. public void ApplyClientBehavior(OperationDescription operationDescription,
     ClientOperation clientOperation) {  
  17. clientOperation.Parent.MessageInspectors.Add(new ContextHeader ());  
  18. }  
  19. public void ApplyDispatchBehavior(OperationDescription operationDescription, 
    DispatchOperation dispatchOperation) {  
  20. }  
  21. public void Validate(OperationDescription operationDescription) {  
  22. }  
  23. #endregion  

 

 

完工大吉,***在我們Contract中加入CallContextAttribute就可以啦,客戶端不用增加任何代碼了。

  1. [ServiceContract]  
  2. [CallContext]  
  3. public interface IBizTest {  
  4. [OperationContract]  
  5. [CallContext]  
  6. string GetWelcomeInfo();  

以上就是我們?yōu)榇蠹医榻B的WCF使用Header的相關(guān)操作方法。

責(zé)任編輯:曹凱 來(lái)源: 博客園
相關(guān)推薦

2010-02-26 11:22:16

LitwareHR使用

2010-02-24 13:48:44

MSMQ使用WCF

2010-02-24 10:07:48

WCF跨越邊界

2010-02-24 10:41:28

WCF服務(wù)保護(hù)

2009-12-21 10:09:26

WCF創(chuàng)建客戶端服務(wù)對(duì)

2010-02-25 13:48:23

WCF動(dòng)態(tài)創(chuàng)建代碼

2010-02-25 09:13:34

WCF異步調(diào)用

2010-02-25 16:52:12

引用WCF服務(wù)

2010-02-26 08:59:10

WCF服務(wù)宿主程序

2009-12-29 18:09:00

Silverlight

2009-12-03 11:11:57

PHP網(wǎng)站優(yōu)化

2009-12-11 17:52:21

PHP獲取博客數(shù)據(jù)

2009-12-07 18:42:55

PHP與Javascr

2009-12-04 12:51:27

PHP functio

2010-02-26 10:30:03

ASP.NET Aja

2009-12-09 16:49:09

PHP顯示文章發(fā)布時(shí)間

2010-01-06 15:56:18

.Net Framew

2010-04-29 17:31:56

Oracle存儲(chǔ)過(guò)程

2009-12-08 14:31:31

PHP命令行讀取參數(shù)

2009-12-15 14:09:39

Ruby創(chuàng)建可參數(shù)化類
點(diǎn)贊
收藏

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