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

知識手冊之談WCF行為擴(kuò)展

開發(fā) 后端
在WCF行為擴(kuò)展是很常用的,對DispatchRuntime和DispatchOperation進(jìn)行擴(kuò)展,擴(kuò)展點(diǎn)包括了對參數(shù)和消息的檢查,以及操作調(diào)用程序。

WCF行為擴(kuò)展有很多值得學(xué)習(xí)的地方,大家可以上網(wǎng)收收資料了解一下,本人比較熱愛WCF這門技術(shù),我先給大家總結(jié)點(diǎn)關(guān)于WCF行為擴(kuò)展的知識。WCF以其靈活的可擴(kuò)展架構(gòu)為開發(fā)者提供了方便,其中WCF行為擴(kuò)展或許是應(yīng)用中最為常見的。自定義對行為的擴(kuò)展并不復(fù)雜,但仍有許多細(xì)節(jié)需要注意。

#T#在服務(wù)端,一般是對DispatchRuntime和DispatchOperation進(jìn)行擴(kuò)展,擴(kuò)展點(diǎn)包括了對參數(shù)和消息的檢查,以及操作調(diào)用程序,它們對應(yīng)的接口分別為IParameterInspector,IDispatchMessageInspector以及 IOperationInvoker。而在客戶端,則是對ClientRuntime和ClientOperation進(jìn)行擴(kuò)展,擴(kuò)展點(diǎn)包括對參數(shù)和消息的檢查,對應(yīng)的接口分別為IParameterInspector和IClientMessageInspector。這些接口類型均被定義在 System.ServiceModel.Dispatcher命名空間下,其中IParameterInspector接口可以同時作用在服務(wù)端和客戶端。

對這些接口的實(shí)現(xiàn),有點(diǎn)類似于AOP的實(shí)現(xiàn),可以對方法調(diào)用前和調(diào)用后注入一些額外的邏輯,所以通常會將這些擴(kuò)展稱為偵聽器。例如IParameterInspector接口,就定義了如下方法:

  1. void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState);  
  2. object BeforeCall(string operationName, object[] inputs); 

在調(diào)用服務(wù)對象的目標(biāo)方法前,會調(diào)用BeforeCall方法,而在調(diào)用后則會調(diào)用AfterCall方法。例如我們可在方法調(diào)用前檢驗(yàn)計(jì)算方法的參數(shù)是否小于0,如果小于0則拋出異常:

  1. public class CalculatorParameterInspector:IParameterInspector  
  2. {  
  3. public void BeforeCall(string operationName, object[] inputs)  
  4. {  
  5. int x = inputs[0] as int;  
  6. int y = inputs[1] as int;  
  7. if (x <0 || y < 0)  
  8. {  
  9. throw new FaultException("The number can not be less than zero.");  
  10. }  
  11. return null;  
  12. }  
  13. public void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState)  
  14. {  
  15. //empty;  
  16. }  

對消息的檢查區(qū)分了服務(wù)端和客戶端,接口方法根據(jù)消息傳遞的順序剛好相反[注]。我們可以通過接口方法對消息進(jìn)行處理,例如打印消息的Header:

  1. public class PrintMessageInterceptor : IDispatchMessageInspector  
  2. {  
  3. #region IDispatchMessageInspector Members  
  4.  
  5. public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, IClientChannel channel, InstanceContext instanceContext)  
  6. {  
  7. MessageBuffer buffer = request.CreateBufferedCopy(Int32.MaxValue);  
  8. request = buffer.CreateMessage();  
  9.  
  10. Console.WriteLine("After Receive Request:");  
  11. foreach (MessageHeader header in request.Headers)  
  12. {  
  13. Console.WriteLine(header);  
  14. }  
  15. Console.WriteLine(new string('*', 20));  
  16. return null;  
  17. }  
  18.  
  19. public void BeforeSendReply(ref System.ServiceModel.Channels.Message reply, object correlationState)  
  20. {  
  21. MessageBuffer buffer = reply.CreateBufferedCopy(Int32.MaxValue);  
  22. reply = buffer.CreateMessage();  
  23.  
  24. Console.WriteLine("Before Send Request:");  
  25. foreach (MessageHeader header in reply.Headers)  
  26. {  
  27. Console.WriteLine(header);  
  28. }  
  29. Console.WriteLine(new string('*', 20));  
  30. }  
  31.  
  32. #endregion  
責(zé)任編輯:田樹 來源: 博客
相關(guān)推薦

2009-03-16 09:16:13

行為擴(kuò)展WCF.NET

2010-02-26 10:46:12

WCF行為擴(kuò)展

2010-03-02 17:48:35

WCF尋址報頭

2009-11-09 17:06:38

WCF選擇綁定

2010-03-01 14:41:14

WCF行為擴(kuò)展

2009-11-05 15:50:25

WCF behavio

2009-12-21 10:44:32

2009-12-22 11:14:38

WCF禁用安全配置

2010-02-25 09:36:28

WCF行為控制

2010-03-01 14:50:30

WCF行為類型

2009-12-22 14:54:52

WCF安全

2010-03-02 09:24:22

WCF變更行為

2010-03-02 14:41:00

WCF行為控制

2009-11-06 14:25:56

WCF接口

2009-12-22 15:33:50

WCF傳輸安全

2009-12-22 14:16:01

WCF連接服務(wù)超時

2010-03-01 09:19:10

WCF編碼規(guī)范

2009-12-22 15:47:03

WCF服務(wù)器證書

2010-02-25 17:22:39

WCF服務(wù)行為

2009-11-05 15:18:19

WCF擴(kuò)展
點(diǎn)贊
收藏

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