如何對(duì)WCF綁定元素進(jìn)行自定義操作
WCF開發(fā)插件在開發(fā)領(lǐng)域中占據(jù)著重要的位置。它可以幫助開發(fā)人員輕松的實(shí)現(xiàn)一個(gè)安全性高及可跨平臺(tái)的企業(yè)級(jí)解決方案。接下來,我們通過一個(gè)案例來演示如果自定義一個(gè)WCF綁定元素。通過該綁定元素來創(chuàng)建我們?cè)谏厦嬉粋€(gè)案例中創(chuàng)建的兩個(gè)自定義信道管理器:SimpleChannelFactory和SimpleChannelListener。按照上面的命名方式,我們把這個(gè)自定義綁定元素命名為:SimpleBindingElement,下面是整個(gè)SimpleBindingElement的定義:
- public class SimpleBindingElement : BindingElement
- {
- public SimpleBindingElement()
- {
- PrintHelper.Print(this, "SimpleBindingElement");
- }
- public override BindingElement Clone()
- {
- PrintHelper.Print(this, "Clone");
- return new SimpleBindingElement();
- }
- public override T GetProperty< T>(BindingContext context)
- {
- PrintHelper.Print(this, string.Format("GetProperty< {0}>",
typeof(T).Name));- return context.GetInnerProperty< T>();
- }
- public override IChannelFactory< TChannel> BuildChannelFactory
< TChannel>(BindingContext context)- {
- PrintHelper.Print(this, "BuildChannelFactory< TChannel>");
- return new SimpleChannelFactory< TChannel>(context) as
IChannelFactory< TChannel>;- }
- public override IChannelListener< TChannel> BuildChannelListener
< TChannel>(BindingContext context)- {
- PrintHelper.Print(this, "BuildChannelListener< TChannel>");
- return new SimpleChannelListener< TChannel>(context) as
IChannelListener< TChannel>;- }
- }
SimpleBindingElement直接繼承自抽象的基類BindingElement,對(duì)SimpleChannelFactory和SimpleChannelListener的創(chuàng)建分別實(shí)現(xiàn)在兩個(gè)被重寫的方法中:BuildChannelFactory< TChannel>和BuildChannelListener< TChannel>中。此外還重寫了兩個(gè)額外的方法:Clone和GetProperty< T>,前者用于克隆一個(gè)新的綁定元素,后一個(gè)和定義在信道、信道管理器的同名方法一樣,用于獲取基于某種類型的屬性。
WCF綁定元素的相關(guān)自定義操作方法就為大家介紹到這里。
【編輯推薦】


















