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

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

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
WCF信道工廠接口與相關(guān)基類(lèi)描述

WCF開(kāi)發(fā)框架中有一種叫做WCF信道工廠的東西。對(duì)于剛剛接觸WCF不久的朋友可能對(duì)其還不太了解。由于信道管理器在客戶端和服務(wù)端所起的不同作用,分為信道監(jiān)聽(tīng)器和信道工廠。#t#

湖州ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書(shū)未來(lái)市場(chǎng)廣闊!成為創(chuàng)新互聯(lián)建站的ssl證書(shū)銷(xiāo)售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18980820575(備注:SSL證書(shū)合作)期待與您的合作!

和服務(wù)端的信道監(jiān)聽(tīng)其相比,處于客戶端的信道工廠顯得簡(jiǎn)單。從名稱(chēng)就可以看得出來(lái),WCF信道工廠的作用就是單純的創(chuàng)建用于消息發(fā)送的信道。我們先來(lái)看看與信道工廠相關(guān)的一些接口和基類(lèi)的定義。

一、WCF信道工廠相關(guān)的接口和基類(lèi)

對(duì)于信道監(jiān)聽(tīng)器,WCF定義了兩個(gè)接口:IChannelListener和IChnnelListener< TChannel>。與之相對(duì)地,WCF也為信道工廠定義了兩個(gè)接口:IChannelFactory和IChannelFactory< TChannel>。這兩個(gè)接口定義了信道工廠最基本的功能和屬性,下面是這兩個(gè)接口的定義:

  1. public interface IChannelFactory 
    : ICommunicationObject  
  2. {  
  3. // Methods  
  4. T GetProperty< T>() where T : class;  
  5. }  
  6. public interface IChannelFactory< TChannel> 
    : IChannelFactory, ICommunicationObject  
  7. {  
  8. // Methods  
  9. TChannel CreateChannel(EndpointAddress to);  
  10. TChannel CreateChannel(EndpointAddress 
    to, Uri via);  
  11. }  

由于WCF信道工廠的目的就是單純的創(chuàng)建信道,所以IChannelFactory和IChannelFactory< TChannel>的定義顯得格外簡(jiǎn)潔。兩個(gè)重載的CreateChannel方法通過(guò)目的終結(jié)點(diǎn)的地址(to),以及在手工尋址下不同于目的終結(jié)點(diǎn)地址的另一個(gè)地址,該地址是消息實(shí)際會(huì)被發(fā)送的地址(via)。關(guān)于To和Via可以參考第二章關(guān)于物理地址和邏輯地址的部分。

除了上面的兩個(gè)接口之外,WCF還定義分別是實(shí)現(xiàn)了它們的兩個(gè)抽象基類(lèi):ChannelFactoryBase和ChannelFactoryBase< TChannel>。

ChannelFactoryBase繼承自所有信道管理器的基類(lèi):CnannelManagerBase,而ChannelManagerBase又繼承自CommunicationObject,實(shí)現(xiàn)ICommunicationObject接口定義的基本的狀態(tài)屬性和狀態(tài)轉(zhuǎn)換功能。并且實(shí)現(xiàn)了接口IChannelFactory和ICommunicationObject。而ChannelFactoryBase< TChannel>繼承自CnannelManagerBase,并且實(shí)現(xiàn)了接口:IChannelFactory< TChannel>, IChannelFactory和ICommunicationObject。

一般地,范型類(lèi)型TChannel為基于相應(yīng)channel shape下客戶端信道類(lèi)型,比如IOutputChannel、IRequestChannel和IDuplexChannel。ChannelFactoryBase和ChannelFactoryBase< TChannel>的簡(jiǎn)單定義如下:

 
 
 
  1. public abstract class ChannelFactoryBase
     : ChannelManagerBase, IChannelFactory, 
    ICommunicationObject  
  2. {  
  3. ......  
  4. }  
  5. public abstract class ChannelFactoryBase
    < TChannel> : ChannelFactoryBase, IChannel
    Factory< TChannel>, IChannelFactory, 
    ICommunicationObject  
  6. {  
  7. ......  
  8. }   

二、案例演示:如何自定義WCF信道工廠

在上一個(gè)案例中,我們創(chuàng)建了一個(gè)自定義的信道監(jiān)聽(tīng)器:SimpleReplyChannelListner。該信道監(jiān)聽(tīng)器用于在請(qǐng)求-回復(fù)消息交換模式下進(jìn)行請(qǐng)求的監(jiān)聽(tīng)。在本案例中,我們來(lái)創(chuàng)建與之相對(duì)的信道工廠:SimpleChannelFactory< TChannel>,用于請(qǐng)求-回復(fù)消息交換模式下進(jìn)行用于請(qǐng)求發(fā)送信道的創(chuàng)建。由于SimpleChannelFactory< TChannel>的實(shí)現(xiàn)相對(duì)簡(jiǎn)單,將所有代碼一并附上。

SimpleChannelFactory< TChannel>直接繼承自抽象基類(lèi)SimpleChannelFactoryBase< TChannel>。字段成員_innerChannelFactory表示信道工廠棧中后一個(gè)信道工廠對(duì)象,該成員在構(gòu)造函數(shù)中通過(guò)傳入的BindingContext對(duì)象的BuildInnerChannelFactory< TChannel>方法創(chuàng)建。

OnCreateChannel是核心大方法,實(shí)現(xiàn)了真正的信道創(chuàng)建過(guò)程,在這里我們創(chuàng)建了我們自定義的信道:SimpleRequestChannel.。構(gòu)建SimpleRequestChannel. 的InnerChannel通過(guò)---_innerChannelFactory的CreateChannel方法創(chuàng)建。對(duì)于其他的方法(OnOpen、OnBeginOpen和OnEndOpen),我們僅僅通過(guò)PrintHelper輸出當(dāng)前的方法名稱(chēng),并調(diào)用-_innerChannelFactory相應(yīng)的方法。

 
 
 
  1. public class SimpleChannelFactory< TChannel>
     : ChannelFactoryBase< TChannel> 
  2. {  
  3. public IChannelFactory< TChannel> _innerChannelFactory;   
  4. public SimpleChannelFactory(BindingContext context)  
  5. {  
  6. PrintHelper.Print(this, "SimpleChannelFactory");  
  7. this._innerChannelFactory = context.
    BuildInnerChannelFactory< TChannel>();  
  8. }   
  9. protected override TChannel OnCreateChannel
    (EndpointAddress address, Uri via)  
  10. {  
  11. PrintHelper.Print(this, "OnCreateChannel");  
  12. IRequestChannel innerChannel = this._
    innerChannelFactory.CreateChannel(address,
     via) as IRequestChannel;  
  13. SimpleRequestChannel. channel = new 
    SimpleRequestChannel.(this, innerChannel);  
  14. return (TChannel)(object)channel;  
  15. }   
  16. protected override IAsyncResult OnBeginOpen
    (TimeSpan timeout, AsyncCallback callback, 
    object state)  
  17. {  
  18. PrintHelper.Print(this, "OnBeginOpen");  
  19. return this._innerChannelFactory.BeginOpen
    (timeout, callback, state);  
  20. }  
  21. protected override void OnEndOpen(IAsyncResult result)  
  22. {  
  23. PrintHelper.Print(this, "OnEndOpen");  
  24. this._innerChannelFactory.EndOpen(result);  
  25. }   
  26. protected override void OnOpen(TimeSpan timeout)  
  27. {  
  28. PrintHelper.Print(this, "OnOpen");  
  29. this._innerChannelFactory.Open(timeout);  
  30. }  
  31. }   

本文題目:WCF信道工廠接口與相關(guān)基類(lèi)描述
文章地址:http://m.5511xx.com/article/djgdcgp.html