/// <summary> /// Author: TerryLee /// Url: [url]http://www.cnblogs.com/terrylee[/url] /// </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()); } }
/// <summary> /// Author: TerryLee /// Url: [url]http://www.cnblogs.com/terrylee[/url] /// </summary> public class FilteringEndpointBehavior : IEndpointBehavior { MessageFilter addressFilter; MessageFilter contractFilter; public FilteringEndpointBehavior(MessageFilter addressFilter, MessageFilter contractFilter) { this.addressFilter = addressFilter; this.contractFilter = contractFilter; } public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters) { } public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime) { throw new InvalidOperationException( "This behavior should only be used on the server."); } public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) { endpointDispatcher.AddressFilter = this.addressFilter; endpointDispatcher.ContractFilter = this.contractFilter; } public void Validate(ServiceEndpoint endpoint) { } }
/// <summary> /// Author: TerryLee /// Url: [url]http://www.cnblogs.com/terrylee[/url] /// </summary> public class FilteringEndpointBehaviorExtension : BehaviorExtensionElement { protected override object CreateBehavior() { MessageFilter addressFilter = new SpecialCharactersMessageFilter(Characters); MessageFilter contractFilter = new MatchAllMessageFilter(); return new FilteringEndpointBehavior(addressFilter, contractFilter); } public override Type BehaviorType { get { return typeof(FilteringEndpointBehavior); } } [ConfigurationProperty("characters", DefaultValue = "terrylee", IsRequired = true)] public String Characters { get { return base["characters"].ToString(); } set { base["characters"] = value; } } }
/// <summary> /// Author: TerryLee /// Url: [url]http://www.cnblogs.com/terrylee[/url] /// </summary> [ServiceContract(Namespace = "http://www.cnblogs.com/terrylee/")] public interface IEchoService { [OperationContract] string Echo(string msg); } public class EchoService : IEchoService { public string Echo(string msg) { return "Hello:" + msg; } }
<extensions> <behaviorExtensions> <add name="filteringEndpointBehavior" type="TerryLee.CustomizeMessageFilter.Service. FilteringEndpointBehaviorExtension, Service, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" /> </behaviorExtensions> </extensions>
<endpointBehaviors> <behavior name="filterBehavior"> <filteringEndpointBehavior characters="terrylee" /> </behavior> </endpointBehaviors>
<endpoint address="" binding ="wsHttpBinding" contract="TerryLee.CustomizeMessageFilter.Service.IEchoService" behaviorConfiguration="filterBehavior"> </endpoint>
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <services> <service name="TerryLee.CustomizeMessageFilter.Service.EchoService" behaviorConfiguration="echoBehavior"> <host> <baseAddresses> <add baseAddress="[url]http://localhost:8887/EchoService[/url]"/> </baseAddresses> </host> <endpoint address="" binding ="wsHttpBinding" contract="TerryLee.CustomizeMessageFilter.Service.IEchoService" behaviorConfiguration="filterBehavior"> </endpoint> </service> </services> <extensions> <behaviorExtensions> <add name="filteringEndpointBehavior" type="TerryLee.CustomizeMessageFilter.Service. FilteringEndpointBehaviorExtension, Service, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" /> </behaviorExtensions> </extensions> <behaviors> <serviceBehaviors> <behavior name="echoBehavior"> <serviceMetadata httpGetEnabled="true"/> </behavior> </serviceBehaviors> <endpointBehaviors> <behavior name="filterBehavior"> <filteringEndpointBehavior characters="terrylee" /> </behavior> </endpointBehaviors> </behaviors> </system.serviceModel> </configuration>
/// <summary> /// Author: TerryLee /// Url: [url]http://www.cnblogs.com/terrylee[/url] /// </summary> static void Main() { Uri serviceVia = new Uri("http://localhost:8887/EchoService"); WSHttpBinding binding = new WSHttpBinding(); ChannelFactory<IEchoService> factory = new ChannelFactory<IEchoService> (binding, new EndpointAddress(serviceVia)); String address = "http://localhost/terrylee"; Console.WriteLine(String.Format("Sending message to {0}...", address)); IEchoService channel = factory.CreateChannel( new EndpointAddress(address), serviceVia); try { String reply = channel.Echo("cnblogs"); Console.WriteLine(reply.ToString()); ((IClientChannel)channel).Close(); } catch (CommunicationException ce) { Console.WriteLine("Exception: {0}", ce.Message); ((IClientChannel)channel).Abort(); } Console.WriteLine("Press <ENTER> to terminate client."); Console.ReadLine(); }
String address = "[url]http://localhost/anytao[/url]";
0javascript
收藏css
Ctrl+Enter 發佈html
發佈java
取消python