日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
通過一個ASP.NET程序模擬WCF基本架構(gòu)

在《WCF技術(shù)剖析》寫作期間,對WCF又有了新的感悟,為此以書名開始本人的第三個WCF系列。本系列的目的在于對《WCF技術(shù)剖析》的補充,會對書中的一些內(nèi)容進行展開講述,同時會囊括很多由于篇幅的原因忍痛割棄的內(nèi)容。

本系列的第一篇,我將會對WCF的基本架構(gòu)作一個大致的講解。不過,一改傳統(tǒng)對WCF的工作流程進行平鋪直敘,我將另辟蹊徑,借助于我們熟悉的ASP.NET作為請求處理平臺,通過一個簡單的托管程序模擬整個WCF客戶端和服務(wù)端的架構(gòu)。本篇文章的大部分內(nèi)容節(jié)選自《WCF技術(shù)剖析(卷1)》第八章。

WCF框架處理流程和涉及的組件

我們的模擬程序?qū)⒛愦罱ㄒ粋€迷你版的WCF框架,為了展示W(wǎng)CF整個處理流程中使用到一些特殊組件。我們首先來簡單介紹一下對于一個簡單的WCF服務(wù)調(diào)用,WCF的客戶端和服務(wù)端框架的處理流程,和該流程的每一個階段都使用那些重要組件。

下面的列表列出了WCF服務(wù)端框架對于處理一個簡單的WCF服務(wù)調(diào)用請求所提供的功能,以及相應(yīng)的功能承載的組件:

請求消息的接收和回復(fù)消息的發(fā)送:服務(wù)端在傳輸層監(jiān)聽與接收來自客戶的請求,并將經(jīng)過編碼后的回復(fù)消息通過傳輸層發(fā)送到客戶端

請求消息的解碼和回復(fù)消息的編碼:將接收到的字節(jié)數(shù)組通過解碼生成請求消息對象,并將回復(fù)消息通過編程轉(zhuǎn)化成字節(jié)組。消息的編碼和解碼通過MessageEncoder完成,而MessageEncoderFactory負責(zé)創(chuàng)建該對象

請求消息的反序列化和回復(fù)消息的序列化:對請求消息進行反序列化,為服務(wù)操作的執(zhí)行生成相應(yīng)的輸入?yún)?shù),以及將服務(wù)操作執(zhí)行的結(jié)果(返回值或者ref/out參數(shù))序列化,并生成回復(fù)消息。序列化和反序列化通過DispatchMessageFormatter完成

服務(wù)對象的創(chuàng)建:創(chuàng)建或者激活服務(wù)對象實例,IntanceProvider用于服務(wù)對象的創(chuàng)建或獲取

服務(wù)操作的執(zhí)行:調(diào)用創(chuàng)建的服務(wù)對象的操作方法,并傳入經(jīng)過反序列化生成的輸入?yún)?shù)。OperationInvoker完成對服務(wù)操作的最終執(zhí)行

較之服務(wù)端的流程,客戶端的流程顯得相對簡單,僅僅包含以下三個必需的階段:

請求消息的序列化和回復(fù)消息的反序列化:生成請求消息并將輸入?yún)?shù)序列化到請求消息中,以及對回復(fù)消息進行反序列化,轉(zhuǎn)化成方法調(diào)用的返回值或者ref/out參數(shù)。序列化和反序列化通過ClienthMessageFormatter完成

請求消息的編碼和回復(fù)消息的解碼:對請求消息進行編碼生成字節(jié)數(shù)組供傳輸層發(fā)送,以及將傳輸層接收到的字節(jié)數(shù)組解碼生成恢復(fù)消息。消息的編碼和解碼通過MessageEncoder完成,而MessageEncoderFactory負責(zé)創(chuàng)建該對象

請求消息的發(fā)送和回復(fù)消息的接收:在傳輸層將經(jīng)過編碼的請求消息發(fā)送到服務(wù)端,以及將接收來自服務(wù)端的恢復(fù)消息

 
圖1 精簡版WCF客戶端與服務(wù)端組件

圖1反映了進行服務(wù)調(diào)用的必要步驟和使用到的相關(guān)WCF組件。在本案例演示中,我們需要做的就是手工創(chuàng)建這些組件,并通過我們自己的代碼利用它們搭建一個建議版的WCF框架。如果讀者能夠?qū)Ρ景咐膶崿F(xiàn)有一個清晰的理解,相信對于整個WCF的框架就不會感到陌生了。

圖2顯示了本案例解決方案的基本結(jié)構(gòu),總共分三個項目。Contracts用于定義服務(wù)契約,被服務(wù)端和客戶端引用。客戶端通過一個Console應(yīng)用模擬,而服務(wù)端則通過一個ASP.NET Website實現(xiàn)。

 
圖2 WCF框架模擬案例應(yīng)用結(jié)構(gòu)

#p#

步驟一、通過服務(wù)契約類型創(chuàng)建相關(guān)組件

WCF在整個服務(wù)調(diào)用生命周期的不同階段,會使用到不同的組件。我們通過一個方法將服務(wù)端和客戶端所需的所有組件都創(chuàng)建出來,為此,我們在Contracts項目中添加了一個Utility類型,在Create< T>方法中創(chuàng)建所有的組件并通過輸出參數(shù)的形式返回,泛型類型T表示的是服務(wù)契約類型。在該方法中,輸出參數(shù)encoderFactory被服務(wù)端和客戶端用于消息的編碼和解碼,clientFormatters和dispatchFormatters以字典的形式包含了基于服務(wù)操作的IClientMessageFormatter和IDispatchMessageFormatter,其中clientFormatters和dispatchFormatters的Key分別為操作名稱和操作對應(yīng)的Action。同樣通過字典形式返回的operationInvokers和methods用于在服務(wù)端執(zhí)行相應(yīng)的操作方法,Key同樣為操作對應(yīng)的Action。

 
 
 
 
  1. public static class Utility
  2. {
  3.     public static void Create< T>(out MessageEncoderFactory encoderFactory,
  4.         out IDictionary< string, IClientMessageFormatter> clientFormatters,
  5.         out IDictionary< string, IDispatchMessageFormatter> dispatchFormatters,
  6.         out IDictionary< string, IOperationInvoker> operationInvokers,
  7.         out IDictionary< string, MethodInfo> methods)
  8.     {
  9.         //省略實現(xiàn)
  10.     }
  11. }

具體的實現(xiàn)如下,由于在WCF框架中使用的MessageEncoderFactory(TextMessageEncoderFactory)、MessageFormatter(DataContractSerializerOperationFormatter)和OperationInvoker(SyncMethodInvoker)都是一些內(nèi)部類型,所以只能通過反射的方式創(chuàng)建它們。而操作名稱和Action也主要通過反射的原理解析應(yīng)用在服務(wù)方法上的OperationContractAttribute得到。

 
 
 
 
  1. public static void Create< T>(out MessageEncoderFactory encoderFactory,
  2.     out IDictionary< string, IClientMessageFormatter> clientFormatters,
  3.     out IDictionary< string, IDispatchMessageFormatter> dispatchFormatters,
  4.     out IDictionary< string, IOperationInvoker> operationInvokers,
  5.     out IDictionary< string, MethodInfo> methods)
  6. {
  7.     //確保類型T是應(yīng)用了ServiceContractAttribute的服務(wù)契約
  8.     object[] attributes = typeof(T).GetCustomAttributes(typeof(ServiceContractAttribute), false);
  9.     if (attributes.Length == 0)
  10.     {
  11.         throw new InvalidOperationException(string.Format("The type \"{0}\" is not a ServiceContract!", typeof(T).AssemblyQualifiedName));
  12.     } 
  13.     //創(chuàng)建字典保存IClientMessageFormatter、IDispatchMessageFormatter、IOperationInvoker和MethodInfo
  14.     clientFormatters = new Dictionary< string, IClientMessageFormatter>();
  15.     dispatchFormatters = new Dictionary< string, IDispatchMessageFormatter>();
  16.     operationInvokers = new Dictionary< string, IOperationInvoker>();
  17.     methods = new Dictionary< string, MethodInfo>(); 
  18.     //MessageEncoderFactory
  19.     string encoderFactoryType = "System.ServiceModel.Channels.TextMessageEncoderFactory,System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
  20.     encoderFactory = (MessageEncoderFactory)Activator.CreateInstance(Type.GetType(encoderFactoryType), MessageVersion.Default, Encoding.UTF8, int.MaxValue, int.MaxValue, new XmlDictionaryReaderQuotas()); 
  21. //得到OperationDecription列表
  22. string defaultNamespace = "http://tempuri.org/";
  23.     ServiceContractAttribute serviceAttribute = (ServiceContractAttribute)attributes[0];
  24.     string serviceNamepace = string.IsNullOrEmpty(serviceAttribute.Namespace) ? defaultNamespace : serviceAttribute.Namespace;
  25.     string serviceName = string.IsNullOrEmpty(serviceAttribute.Name) ? typeof(T).Name : serviceAttribute.Name;
  26.     var operations = ContractDescription.GetContract(typeof(T)).Operations; 
  27.     //得到具體的IClientMessageFormatter、IDispatchMessageFormatter和IOperationInvoker的具體類型
  28.     //IClientMessageFormatter+IDispatchMessageFormatter:DataContractSerializerOperationFormatter
  29.     //IOperationInvoker:SyncMethodInvoker
  30.     string formatterTypeName = "System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter,System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
  31.     Type formatterType = Type.GetType(formatterTypeName);
  32.     ConstructorInfo formatterConstructor = formatterType.GetConstructor(new Type[] { typeof(OperationDescription), typeof(DataContractFormatAttribute), typeof(DataContractSerializerOperationBehavior) });
  33.     string operationInvokerTypeName = "System.ServiceModel.Dispatcher.SyncMethodInvoker,System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
  34.     Type operationInvokerType = Type.GetType(operationInvokerTypeName); 
  35.     foreach (MethodInfo method in typeof(T).GetMethods())
  36.     {
  37.         attributes = method.GetCustomAttributes(typeof(OperationContractAttribute), true);
  38.         if (attributes.Length > 0)
  39.         {
  40.             OperationContractAttribute operationAttribute = (OperationContractAttribute)attributes[0];
  41.             string operationName = string.IsNullOrEmpty(operationAttribute.Name) ? method.Name : operationAttribute.Name;
  42.             //通過OperationContractAttribute得到Action
  43.             string action;
  44.             if (string.IsNullOrEmpty(operationAttribute.Action))
  45.             {
  46.                 action = string.Format("{0}{1}/{2}", serviceNamepace, serviceName, operationName);
  47.             }
  48.             else
  49.             {
  50.                 action = operationAttribute.Action;
  51.             } 
  52.             OperationDescription operation = operations.Where(op => op.Name == operationName).ToArray< OperationDescription>()[0];
  53.             //通過反射創(chuàng)建DataContractSerializerOperationFormatter對象
  54.             object formatter = formatterConstructor.Invoke(new object[] { operation, new DataContractFormatAttribute(), null });
  55.             clientFormatters.Add(operationName, formatter as IClientMessageFormatter);
  56.             dispatchFormatters.Add(action, formatter as IDispatchMessageFormatter); 
  57.             //通過反射創(chuàng)建SyncMethodInvoker對象
  58.             IOperationInvoker operationInvoker = (IOperationInvoker)Activator.CreateInstance(operationInvokerType, method);
  59.             operationInvokers.Add(action, operationInvoker);
  60.             methods.Add(action, method);
  61.         }
  62. }

步驟二、創(chuàng)建服務(wù)契約和實現(xiàn)服務(wù)

接下來為本案例創(chuàng)建一個服務(wù)契約和實現(xiàn)該契約。服務(wù)契約定義在Contracts項目,具體的服務(wù)實現(xiàn)在模擬服務(wù)端的ASP.NET Web站點中。簡單起見,依然沿用計算服務(wù)的例子。

 
 
 
 
  1. namespace Artech.WcfFrameworkSimulator.Contracts
  2. {
  3.     [ServiceContract(Namespace = "http://www.artech.com/")]
  4.     public interface ICalculator
  5.     {
  6.         [OperationContract]
  7.         double Add(double x, double y);
  8.     }
  9. }
  10.  
  11. public class CalculatorService : ICalculator
  12. {
  13.     public double Add(double x, double y)
  14.     {
  15.         return x + y;
  16.     }
  17. }

#p#

步驟三、實現(xiàn)服務(wù)端對服務(wù)調(diào)用請求的處理

我們通過一個ASP.NET的Web Page來模擬WCF服務(wù)端對服務(wù)請求的處理,下面的Calculator類型相關(guān)的代碼實際上就是Calculator.aspx的后臺代碼(Code Behind)。整個處理流程不算復(fù)雜。在構(gòu)造函數(shù)中,調(diào)用Utility的Create< ICalculator>方法,將所需的組件進行初始化,而具體的服務(wù)調(diào)用請求處理的邏輯在直接寫在Web Page的Load事件中。

首先,通過MessageCoderFactory創(chuàng)建MessageEncoder對接收到的以HttpRequest形式體現(xiàn)的服務(wù)調(diào)用請求進行解碼,并生成請求消息。通過請求消息得到當(dāng)前服務(wù)操作的Action屬性后,在初始化過程中得到的基于服務(wù)契約所有MethodInfo列表中,根據(jù)該Action得到當(dāng)前操作對應(yīng)的MethodInfo對象。借助于MethodInfo對象得到操作方法的輸入?yún)?shù)和輸出參數(shù)數(shù)量后,創(chuàng)建兩個對象數(shù)組,分別用于保存通過DispatchMessageFormatter對象對于請求消息進行反序列化得到的輸入?yún)?shù),和通過OperationInvoker執(zhí)行操作方法得到的輸出參數(shù)。在OperationInvoker執(zhí)行操作方法之前,通過反射的方式直接創(chuàng)建服務(wù)對象,這一步在真正的WCF框架中是通過InstanceProvider實現(xiàn)的。

通過OperationInvoker執(zhí)行操作方法的結(jié)果有兩種形式:返回值和輸出參數(shù)(包括引用參數(shù))。它們通過被傳入DispatchMessageFormatter被序列化并生成回復(fù)消息對象?;貜?fù)消息通過MessageCoderFactory創(chuàng)建MessageEncoder進行編碼后通過HttpResponse返回。

 
 
 
 
  1. public partial class Calculator : System.Web.UI.Page
  2. {
  3.     private static MessageVersion messageversion = MessageVersion.Default;
  4.     private static MessageEncoderFactory encoderFactory;
  5.     private static IDictionary< string, IDispatchMessageFormatter> dispatchFormatters;
  6.     private static IDictionary< string, IOperationInvoker> operationInvokers;
  7.     private static IDictionary< string, MethodInfo> methods;
  8.     protected Calculator()
  9.     {
  10.         IDictionary< string, IClientMessageFormatter> clientFormatters;
  11.         Utility.Create< ICalculator>(out encoderFactory, out clientFormatters, out dispatchFormatters, out operationInvokers, out methods);
  12.     }
  13.     protected void Page_Load(object sender, EventArgs e)
  14.     {
  15.         //對HttpPRequest進行解碼生成請求消息對象
  16.         Message request = encoderFactory.Encoder.ReadMessage(this.Request.InputStream, int.MaxValue, "application/soap+xml; charset=utf-8");
  17.         //通過請求消息得到代表服務(wù)操作的Action
  18.         string action = request.Headers.Action;
  19.         //通過Action從MethodInfo字典中獲取服務(wù)操作對應(yīng)的MethodInfo對象
  20.         MethodInfo method = methods[action];
  21.         //得到輸出參數(shù)的數(shù)量
  22.         int outArgsCount = 0;
  23.         foreach (var parameter in method.GetParameters())
  24.         {
  25.             if (parameter.IsOut)
  26.             {
  27.                 outArgsCount++;
  28.             }
  29.         }
  30.         //創(chuàng)建數(shù)組容器,用于保存請求消息反序列后生成的輸入?yún)?shù)對象
  31.         int inputArgsCount = method.GetParameters().Length - outArgsCount;
  32.         object[] parameters = new object[inputArgsCount];
  33.         dispatchFormatters[action].DeserializeRequest(request, parameters);
  34.         List< object> inputArgs = new List< object>();
  35.         object[] outArgs = new object[outArgsCount];
  36.         //創(chuàng)建服務(wù)對象,在WCF中服務(wù)對象通過InstanceProvider創(chuàng)建
  37.         object serviceInstance = Activator.CreateInstance(typeof(CalculatorService));
  38.         //執(zhí)行服務(wù)操作
  39.         object result = operationInvokers[action].Invoke(serviceInstance, parameters, out outArgs);
  40.         //將操作執(zhí)行的結(jié)果(返回值或者輸出參數(shù))序列化生成回復(fù)消息
  41.         Message reply = dispatchFormatters[action].SerializeReply(messageversion, outArgs, result);
  42.         this.Response.ClearContent();
  43.         this.Response.ContentEncoding = Encoding.UTF8;
  44.         this.Response.ContentType = "application/soap+xml; charset=utf-8";
  45.         //對回復(fù)消息進行編碼,并將編碼后的消息通過HttpResponse返回
  46.         encoderFactory.Encoder.WriteMessage(reply, this.Response.OutputStream);
  47.         this.Response.Flush();
  48.     }
  49. }

步驟四、實現(xiàn)客戶端對服務(wù)調(diào)用請求的處理

由于在客戶端對服務(wù)請求的處理是通過一個RealProxy(ServiceChannelFactory)實現(xiàn)的,為了真實模擬WCF處理框架,在這里通過一個自定義RealProxy來實現(xiàn)客戶端相關(guān)的服務(wù)調(diào)用請求的處理。下面代碼中定義的ServiceRealProxy< IContract>就是這樣一個自定義RealProxy。

用于處理服務(wù)調(diào)用請求的相關(guān)組件對象,比如MessageEncoderFactory和IClientMessageFormatter字典,以及所需的屬性,比如消息的版本和服務(wù)的目的地址,通過構(gòu)造函數(shù)指定。而具體的請求處理實現(xiàn)在重寫的Invoke方法之中。首先通過解析應(yīng)用在當(dāng)前方法的上面的OperationContractAttribute得到服務(wù)操作的名稱,以此為Key從IClientMessageFormatter字典中得到當(dāng)前服務(wù)操作對應(yīng)的IClientMessageFormatter對象。當(dāng)前操作方法調(diào)用的輸入?yún)?shù)通過IClientMessageFormatter對象進行序列化后生成請求消息。為請求消息添加必要的尋址報頭后,通過MessageEncoderFactory創(chuàng)建的MessageEncoder對請求消息進行編碼。經(jīng)過編碼的消息以HttpRequest的形式發(fā)送到服務(wù)端,從而完成了服務(wù)調(diào)用請求的發(fā)送。

服務(wù)調(diào)用的結(jié)果通過HttpResponse的形式返回后,先通過MessageEncoder對其解碼,并生成回復(fù)消息?;貜?fù)消息通過IClientMessageFormatter進行反序列化后,在消息中以XML InfoSet實行體現(xiàn)的結(jié)果被轉(zhuǎn)化成具體的對象,這些對象被最終影射為方法調(diào)用的返回值和輸出參數(shù)(包含引用參數(shù))。

 
 
 
 
  1. namespace Artech.WcfFrameworkSimulator.Client
  2. {
  3.     public class ServiceRealProxy< IContract> : RealProxy
  4.     {
  5.         private Uri _remoteAddress;
  6.         private IDictionary< string, IClientMessageFormatter> _messageFormatters;
  7.         private MessageVersion _messageVersion = MessageVersion.Default;
  8.         private MessageEncoderFactory _messageEncoderFactory;
  9.         public ServiceRealProxy(MessageVersion messageVersion, Uri address, IDictionary< string, IClientMessageFormatter> messageFormaters, MessageEncoderFactory messageEncoderFactory)
  10.             : base(typeof(IContract))
  11.         {
  12.             object[] attribute = typeof(IContract).GetCustomAttributes(typeof(ServiceContractAttribute), false);
  13.             if (attribute.Length == 0)
  14.             {
  15.                 throw new InvalidOperationException(string.Format("The type \"{0}\" is not a ServiceContract!", typeof(IContract).AssemblyQualifiedName));
  16.             }
  17.             this._messageVersion = messageVersion;
  18.             this._remoteAddress = address;
  19.             this._messageFormatters = messageFormaters;
  20.             this._messageEncoderFactory = messageEncoderFactory;
  21.         }
  22.         public override IMessage Invoke(IMessage msg)
  23.         {
  24.             IMethodCallMessage methodCall = (IMethodCallMessage)msg;
  25.             //Get Operation name.
  26.             object[] attributes = methodCall.MethodBase.GetCustomAttributes(typeof(OperationContractAttribute), true);
  27.             if (attributes.Length == 0)
  28.             {
  29.                 throw new InvalidOperationException(string.Format("The method \"{0}\" is not a valid OperationContract.", methodCall.MethodName));
  30.             }
  31.             OperationContractAttribute attribute = (OperationContractAttribute)attributes[0];
  32.             string operationName = string.IsNullOrEmpty(attribute.Name) ? methodCall.MethodName : attribute.Name;
  33.             //序列化請求消息
  34.             Message requestMessage = this._messageFormatters[operationName].SerializeRequest(this._messageVersion, methodCall.InArgs);
  35.             //添加必要的WS-Address報頭
  36.             EndpointAddress address = new EndpointAddress(this._remoteAddress);
  37.             requestMessage.Headers.MessageId = new UniqueId(Guid.NewGuid());
  38.             requestMessage.Headers.ReplyTo = new EndpointAddress("http://www.w3.org/2005/08/addressing/anonymous");
  39.             address.ApplyTo(requestMessage);
  40.             //對請求消息進行編碼,并將編碼生成的字節(jié)發(fā)送通過HttpWebRequest向服務(wù)端發(fā)送
  41.             HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(this._remoteAddress);
  42.             webRequest.Method = "Post";
  43.             webRequest.KeepAlive = true;
  44.             webRequest.ContentType = "application/soap+xml; charset=utf-8";
  45.             ArraySegment< byte> bytes = this._messageEncoderFactory.Encoder.WriteMessage(requestMessage, int.MaxValue, BufferManager.CreateBufferManager(long.MaxValue, int.MaxValue));
  46.             webRequest.ContentLength = bytes.Array.Length;
  47.             webRequest.GetRequestStream().Write(bytes.Array, 0, bytes.Array.Length);
  48.             webRequest.GetRequestStream().Close();
  49.             WebResponse webResponse = webRequest.GetResponse();
  50.             //對HttpResponse進行解碼生成回復(fù)消息.
  51.             Message responseMessage = this._messageEncoderFactory.Encoder.ReadMessage(webResponse.GetResponseStream(), int.MaxValue);
  52.             //回復(fù)消息進行反列化生成相應(yīng)的對象,并映射為方法調(diào)用的返回值或者ref/out參數(shù)
  53.             object[] allArgs = (object[])Array.CreateInstance(typeof(object), methodCall.ArgCount);
  54.             Array.Copy(methodCall.Args, allArgs, methodCall.ArgCount);
  55.             object[] refOutParameters = new object[GetRefOutParameterCount(methodCall.MethodBase)];
  56.             object returnValue = this._messageFormatters[operationName].DeserializeReply(responseMessage, refOutParameters);
  57.             MapRefOutParameter(methodCall.MethodBase, allArgs, refOutParameters);
  58.             //通過ReturnMessage的形式將返回值和ref/out參數(shù)返回
  59.             return new ReturnMessage(returnValue, allArgs, allArgs.Length, methodCall.LogicalCallContext, methodCall);
  60.         }
  61.         private int GetRefOutParameterCount(MethodBase method)
  62.         {
  63.             int count = 0;
  64.             foreach (ParameterInfo parameter in method.GetParameters())
  65.             {
  66.                 if (parameter.IsOut || parameter.ParameterType.IsByRef)
  67.                 {
  68.                     count++;
  69.                 }
  70.             }
  71.             return count;
  72.         }
  73.         private void MapRefOutParameter(MethodBase method, object[] allArgs, object[] refOutArgs)
  74.         {
  75.             List< int> refOutParamPositionsList = new List< int>();
  76.             foreach (ParameterInfo parameter in method.GetParameters())
  77.             {
  78.                 if (parameter.IsOut || parameter.ParameterType.IsByRef)
  79.                 {
  80.                     refOutParamPositionsList.Add(parameter.Position);
  81.                 }
  82.             }
  83.             int[] refOutParamPositionArray = refOutParamPositionsList.ToArray();
  84.             for (int i = 0; i <  refOutArgs.Length; i++)
  85.             {
  86.                 allArgs[refOutParamPositionArray[i]] = refOutArgs[i];
  87.             }
  88.         }
  89.     }
  90. }

在真正的WCF客戶端框架下,客戶端通過ChannelFactory< T>創(chuàng)建服務(wù)代理對象進行服務(wù)的調(diào)用,在這里我們也創(chuàng)建一個完成相似功能的工廠類型: SerivceProxyFactory< T>,泛型類型T代表服務(wù)契約類型。

用于創(chuàng)建服務(wù)代理的Create方法很簡單:先通過Utility.Create< T>方法創(chuàng)建客戶端進行服務(wù)調(diào)用必須的相關(guān)組件對象,通過這些對象連同該方法的參數(shù)(消息版本和服務(wù)目的地址)創(chuàng)建ServiceRealProxy< T>對象,最終返回的是該RealProxy的TransparentProxy。

 
 
 
 
  1. namespace Artech.WcfFrameworkSimulator.Client
  2. {
  3.     public static class SerivceProxyFactory< T>
  4.     {
  5.         public static T Create(MessageVersion messageVersion, Uri remoteAddress)
  6.         {
  7.             MessageEncoderFactory encoderFactory;
  8.             IDictionary< string, IClientMessageFormatter> clientFormatters;
  9.             IDictionary< string, IDispatchMessageFormatter> dispatchFormatters;
  10.             IDictionary< string, IOperationInvoker> operationInvokers;
  11.             IDictionary< string, MethodInfo> methods;
  12.             Utility.Create< T>(out encoderFactory, out clientFormatters, out dispatchFormatters, out operationInvokers, out methods);
  13.             ServiceRealProxy< T> realProxy = new ServiceRealProxy< T>(messageVersion, remoteAddress, clientFormatters, encoderFactory);
  14.             return (T)realProxy.GetTransparentProxy();
  15.         }
  16.     }
  17. }

那么在最終的客戶端代碼中就可以借助SerivceProxyFactory< T>創(chuàng)建服務(wù)代理進行服務(wù)調(diào)用了,而這里服務(wù)的目標(biāo)地址實際上是上面用于模擬WCF服務(wù)端框架的.aspx Web Page的地址。

 
 
 
 
  1. namespace Artech.WcfFrameworkSimulator.Client
  2. {
  3.     class Program
  4.     {
  5.         static void Main(string[] args)
  6.         {
  7.             ICalculator calculator = SerivceProxyFactory< ICalculator>.Create(MessageVersion.Default, new Uri("http://localhost/Artech.WcfFrameworkSimulator/Calculator.aspx"));
  8.             double result = calculator.Add(1, 2);
  9.             Console.WriteLine("x + y = {2} when x = {0} and y = {1}", 1, 2, result);
  10.         }
  11.     }
  12. }

執(zhí)行結(jié)果:

x + y = 3 when x = 1 and y = 2


當(dāng)前文章:通過一個ASP.NET程序模擬WCF基本架構(gòu)
鏈接地址:http://m.5511xx.com/article/ccdscdj.html