WCF自定義過濾器相關(guān)實(shí)現(xiàn)方法簡介
在這篇文章中,我們將會為大家詳細(xì)介紹一下有關(guān)WCF自定義過濾器的相關(guān)實(shí)現(xiàn)技巧以及應(yīng)用方式。對于又需要的初學(xué)者們,可以通過本文介紹的內(nèi)容充分掌握這一應(yīng)用技巧,解決開發(fā)過程中遇到的問題。
在默認(rèn)情況下,默認(rèn)情況下,僅當(dāng)消息的“To”標(biāo)頭為終結(jié)點(diǎn)的 EndpointAddress 并且消息的動作與終結(jié)點(diǎn)操作的動作之一匹配時,終結(jié)點(diǎn)的消息篩選器才與此消息匹配。在本文中,我們將自定義一個消息過濾器,它不要求消息的“To”標(biāo)頭完全與EndpointAddress完全匹配,而只是檢測SOAP消息中的“To”標(biāo)頭中是否包含某些特定的字符。所有的消息過濾器都從MessageFilter基類繼承,WCF自定義過濾器的實(shí)現(xiàn)如下代碼所示:
- /// < summary>
- /// Author: TerryLee
- /// Url: http://www.cnblogs.com/terrylee
- /// < /summary>
- public class SpecialCharactersMessageFilter : MessageFilter
- {
- private String _characters = String.Empty;
- public SpecialCharactersMessageFilter(string characters)
- {
- this._characters = characters;
- }
- public override bool Match(Message message)
- {
- Uri to = message.Headers.To;
- if (to == null)
- return false;
- return to.AbsoluteUri.Contains(_characters);
- }
- public override bool Match(MessageBuffer buffer)
- {
- return Match(buffer.CreateMessage());
- }
- }
SpecialCharactersMessageFilter的實(shí)現(xiàn)非常簡單,僅僅是查找“To”標(biāo)頭是否包含某些特定字符,這些字符我們會在配置文件中進(jìn)行配置。
以上就是我們?yōu)榇蠹以敿?xì)介紹的WCF自定義過濾器相關(guān)應(yīng)用技術(shù)。
【編輯推薦】