新聞中心
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è)接口的定義:
- public interface IChannelFactory
: ICommunicationObject - {
- // Methods
- T GetProperty< T>() where T : class;
- }
- public interface IChannelFactory< TChannel>
: IChannelFactory, ICommunicationObject - {
- // Methods
- TChannel CreateChannel(EndpointAddress to);
- TChannel CreateChannel(EndpointAddress
to, Uri via); - }
由于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)單定義如下:
- public abstract class ChannelFactoryBase
: ChannelManagerBase, IChannelFactory,
ICommunicationObject- {
- ......
- }
- public abstract class ChannelFactoryBase
< TChannel> : ChannelFactoryBase, IChannel
Factory< TChannel>, IChannelFactory,
ICommunicationObject- {
- ......
- }
二、案例演示:如何自定義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)的方法。
- public class SimpleChannelFactory< TChannel>
: ChannelFactoryBase< TChannel>- {
- public IChannelFactory< TChannel> _innerChannelFactory;
- public SimpleChannelFactory(BindingContext context)
- {
- PrintHelper.Print(this, "SimpleChannelFactory");
- this._innerChannelFactory = context.
BuildInnerChannelFactory< TChannel>();- }
- protected override TChannel OnCreateChannel
(EndpointAddress address, Uri via)- {
- PrintHelper.Print(this, "OnCreateChannel");
- IRequestChannel innerChannel = this._
innerChannelFactory.CreateChannel(address,
via) as IRequestChannel;- SimpleRequestChannel. channel = new
SimpleRequestChannel.(this, innerChannel);- return (TChannel)(object)channel;
- }
- protected override IAsyncResult OnBeginOpen
(TimeSpan timeout, AsyncCallback callback,
object state)- {
- PrintHelper.Print(this, "OnBeginOpen");
- return this._innerChannelFactory.BeginOpen
(timeout, callback, state);- }
- protected override void OnEndOpen(IAsyncResult result)
- {
- PrintHelper.Print(this, "OnEndOpen");
- this._innerChannelFactory.EndOpen(result);
- }
- protected override void OnOpen(TimeSpan timeout)
- {
- PrintHelper.Print(this, "OnOpen");
- this._innerChannelFactory.Open(timeout);
- }
- }
本文題目:WCF信道工廠接口與相關(guān)基類(lèi)描述
文章地址:http://m.5511xx.com/article/djgdcgp.html


咨詢(xún)
建站咨詢(xún)
