預設 EndpointAddressMessageFilter 要求 To 標頭完全匹配。業務需求:僅檢查 To 標頭是否包含特定字串(如 "terrylee")即路由至端點。
繼承 MessageFilter 覆寫 Match:
public class SpecialCharactersMessageFilter : MessageFilter
{
private readonly string _characters;
public SpecialCharactersMessageFilter(string characters) => _characters = characters;
public override bool Match(Message message)
{
var to = message.Headers.To;
return to != null && to.AbsoluteUri.Contains(_characters);
}
public override bool Match(MessageBuffer buffer) => Match(buffer.CreateMessage());
}
public class FilteringEndpointBehavior : IEndpointBehavior
{
private readonly MessageFilter _addressFilter, _contractFilter;
public FilteringEndpointBehavior(MessageFilter addressFilter, MessageFilter contractFilter)
{ _addressFilter = addressFilter; _contractFilter = contractFilter; }
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher dispatcher)
{
dispatcher.AddressFilter = _addressFilter;
dispatcher.ContractFilter = _contractFilter;
}
// 其他介面方法空實作...
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
=> throw new InvalidOperationException("僅限服務端使用");
}
public class FilteringEndpointBehaviorExtension : BehaviorExtensionElement
{
[ConfigurationProperty("characters", DefaultValue = "terrylee", IsRequired = true)]
public string Characters => (string)base["characters"];
protected override object CreateBehavior()
{
return new FilteringEndpointBehavior(
new SpecialCharactersMessageFilter(Characters),
new MatchAllMessageFilter()); // 合約篩選器全通
}
public override Type BehaviorType => typeof(FilteringEndpointBehavior);
}
[ServiceContract(Namespace = "https://www.cnblogs.com/terrylee/")]
public interface IEchoService { [OperationContract] string Echo(string msg); }
public class EchoService : IEchoService { public string Echo(string msg) => "Hello:" + msg; }
<system.serviceModel>
<services>
<service name="...EchoService" behaviorConfiguration="echoBehavior">
<endpoint address="" binding="wsHttpBinding" contract="...IEchoService"
behaviorConfiguration="filterBehavior" />
</service>
</services>
<extensions>
<behaviorExtensions>
<add name="filteringEndpointBehavior"
type="...FilteringEndpointBehaviorExtension, Service, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</behaviorExtensions>
</extensions>
<behaviors>
<endpointBehaviors>
<behavior name="filterBehavior">
<filteringEndpointBehavior characters="terrylee" />
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
啟動服務,主控台輸出確認篩選器已替換:

var factory = new ChannelFactory<IEchoService>(new WSHttpBinding(),
new EndpointAddress("https://localhost/terrylee")); // 含 terrylee
var channel = factory.CreateChannel(new EndpointAddress("https://localhost/terrylee"),
new Uri("https://localhost:8887/EchoService")); // Via 實體位址
Console.WriteLine(channel.Echo("cnblogs")); // 成功

new EndpointAddress("https://localhost/anytao") // 無 terrylee
回傳錯誤:

自訂 MessageFilter + IEndpointBehavior + BehaviorExtensionElement 三步驟,靈活控制訊息路由邏輯,脫離預設完全匹配限制,構建豐富中介路由場景。