如何對WCF綁定元素進行自定義操作
WCF開發(fā)插件在開發(fā)領域中占據(jù)著重要的位置。它可以幫助開發(fā)人員輕松的實現(xiàn)一個安全性高及可跨平臺的企業(yè)級解決方案。接下來,我們通過一個案例來演示如果自定義一個WCF綁定元素。通過該綁定元素來創(chuàng)建我們在上面一個案例中創(chuàng)建的兩個自定義信道管理器:SimpleChannelFactory和SimpleChannelListener。按照上面的命名方式,我們把這個自定義綁定元素命名為:SimpleBindingElement,下面是整個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,對SimpleChannelFactory和SimpleChannelListener的創(chuàng)建分別實現(xiàn)在兩個被重寫的方法中:BuildChannelFactory< TChannel>和BuildChannelListener< TChannel>中。此外還重寫了兩個額外的方法:Clone和GetProperty< T>,前者用于克隆一個新的綁定元素,后一個和定義在信道、信道管理器的同名方法一樣,用于獲取基于某種類型的屬性。
WCF綁定元素的相關自定義操作方法就為大家介紹到這里。
【編輯推薦】