WCF行為擴(kuò)展正確內(nèi)容應(yīng)用技巧分享
WCF中的一些行為應(yīng)用在實(shí)際應(yīng)用中是一個(gè)非常重要的知識(shí)點(diǎn)。我們今天將會(huì)針對(duì)WCF行為擴(kuò)展的相關(guān)內(nèi)容做一個(gè)詳細(xì)的介紹,希望對(duì)此又需要的朋友們可以通過本文介紹的內(nèi)容充分掌握這一應(yīng)用技術(shù)。#t#
WCF以其靈活的可擴(kuò)展架構(gòu)為開發(fā)者提供了方便,其中對(duì)行為的擴(kuò)展或許是應(yīng)用中最為常見的。自 定義對(duì)WCF行為擴(kuò)展并不復(fù)雜,但仍有許多細(xì)節(jié)需要注意。在服務(wù)端,一般是對(duì)DispatchRuntime和DispatchOperation進(jìn)行擴(kuò)展, 擴(kuò)展點(diǎn)包括了對(duì)參數(shù)和消息的檢查,以及操作調(diào)用程序,它們對(duì)應(yīng)的接口分別為 IParameterInspector,IDispatchMessageInspector以及IOperationInvoker。而在客戶端,則是對(duì)ClientRuntime和ClientOperation進(jìn)行擴(kuò)展,擴(kuò)展點(diǎn)包括對(duì)參數(shù)和消息的檢查,對(duì)應(yīng)的接口分別為 IParameterInspector和IClientMessageInspector。這些接口類型均被定義在 System.ServiceModel.Dispatcher命名空間下,其中IParameterInspector接口可以同時(shí)作用在服務(wù)端和客戶 端。
對(duì)這些接口的實(shí)現(xiàn),有點(diǎn)類似于AOP的實(shí)現(xiàn),可以對(duì)方法調(diào)用前和調(diào)用后注入一些額外的邏輯,所以通常會(huì)將這些WCF行為擴(kuò)展稱為偵聽器。例如IParameterInspector接口,就定義了如下方法:
- void AfterCall(string operationName, object[] outputs,
object returnValue, object correlationState);- object BeforeCall(string operationName, object[] inputs);
在調(diào)用服務(wù)對(duì)象的目標(biāo)方法前,會(huì)調(diào)用BeforeCall方法,而在調(diào)用后則會(huì)調(diào)用AfterCall方法。例如我們可在方法調(diào)用前檢驗(yàn)計(jì)算方法的參數(shù)是否小于0,如果小于0則拋出異常:
- public class CalculatorParameterInspector:IParameterInspector
- {
- public void BeforeCall(string operationName, object[] inputs)
- {
- int x = inputs[0] as int;
- int y = inputs[1] as int;
- if (x <0 || y < 0)
- {
- throw new FaultException("The number can not be less than zero.");
- }
- return null;
- }
- public void AfterCall(string operationName, object[] outputs,
object returnValue, object correlationState)- {
- //empty;
- }
- }
對(duì)消息的檢查區(qū)分了服務(wù)端和客戶端,接口方法根據(jù)消息傳遞的順序剛好相反[注]。我們可以通過接口方法對(duì)消息進(jìn)行處理,例如打印消息的Header:
- public class PrintMessageInterceptor : IDispatchMessageInspector
- {
- #region IDispatchMessageInspector Members
- public object AfterReceiveRequest(ref System.ServiceModel.Channels.
Message request, IClientChannel channel, InstanceContext instanceContext)- {
- MessageBuffer buffer = request.CreateBufferedCopy(Int32.MaxValue);
- request = buffer.CreateMessage();
- Console.WriteLine("After Receive Request:");
- foreach (MessageHeader header in request.Headers)
- {
- Console.WriteLine(header);
- }
- Console.WriteLine(new string('*', 20));
- return null;
- }
- public void BeforeSendReply(ref System.ServiceModel.Channels.
Message reply, object correlationState)- {
- MessageBuffer buffer = reply.CreateBufferedCopy(Int32.MaxValue);
- reply = buffer.CreateMessage();
- Console.WriteLine("Before Send Request:");
- foreach (MessageHeader header in reply.Headers)
- {
- Console.WriteLine(header);
- }
- Console.WriteLine(new string('*', 20));
- }
- #endregion
- }
WCF提供了四種類型的行為:服務(wù)行為、終結(jié)點(diǎn)行為、契約行為和操作行為。 這四種WCF行為擴(kuò)展分別定義了四個(gè)接口:IServiceBehavior,IEndpointBehavior,IContractBehavior以及 IOperationBehavior。雖然是四個(gè)不同的接口,但它們的接口方法卻基本相同,分別為 AddBindingParameters(),ApplyClientBehavior()以及ApplyDispatchBehavior()。注 意,IServiceBehavior由于只能作用在服務(wù)端,因此并不包含ApplyClientBehavior()方法。