日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

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

新聞中心

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

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

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

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

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

對(duì)于信道監(jiān)聽器,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>的定義顯得格外簡潔。兩個(gè)重載的CreateChannel方法通過目的終結(jié)點(diǎn)的地址(to),以及在手工尋址下不同于目的終結(jié)點(diǎn)地址的另一個(gè)地址,該地址是消息實(shí)際會(huì)被發(fā)送的地址(via)。關(guān)于To和Via可以參考第二章關(guān)于物理地址和邏輯地址的部分。

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

ChannelFactoryBase繼承自所有信道管理器的基類: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。

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

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

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

OnCreateChannel是核心大方法,實(shí)現(xiàn)了真正的信道創(chuàng)建過程,在這里我們創(chuàng)建了我們自定義的信道:SimpleRequestChannel.。構(gòu)建SimpleRequestChannel. 的InnerChannel通過---_innerChannelFactory的CreateChannel方法創(chuàng)建。對(duì)于其他的方法(OnOpen、OnBeginOpen和OnEndOpen),我們僅僅通過PrintHelper輸出當(dā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)基類描述
當(dāng)前網(wǎng)址:http://www.dlmjj.cn/article/djgdcgp.html