namespace Microsoft.ServiceModel.Samples.Discovery.Protocol { using System; using System.Text; using System.Collections.Generic; using System.ServiceModel.Channels; public class CompositeTransportBindingElement : TransportBindingElement { public CompositeTransportBindingElement(Binding channelBinding, Binding listenerBinding) { this.channelBinding = channelBinding; this.listenerBinding = listenerBinding; } public CompositeTransportBindingElement(CompositeTransportBindingElement other) : base(other) { this.channelBinding = new CustomBinding(other.channelBinding); this.listenerBinding = new CustomBinding(other.listenerBinding); } public override bool CanBuildChannelFactory(BindingContext context) { if (context == null) { throw new ArgumentNullException("context"); } return channelBinding.CanBuildChannelFactory(context.BindingParameters); } public override bool CanBuildChannelListener(BindingContext context) { if (context == null) { throw new ArgumentNullException("context"); } return listenerBinding.CanBuildChannelListener(context.BindingParameters); } public override IChannelFactory BuildChannelFactory(BindingContext context) { return channelBinding.BuildChannelFactory(context.BindingParameters); } public override IChannelListener BuildChannelListener(BindingContext context) { return listenerBinding.BuildChannelListener( context.ListenUriBaseAddress, context.ListenUriRelativeAddress, context.ListenUriMode, context.BindingParameters); } public override BindingElement Clone() { return new CompositeTransportBindingElement(this); } public override T GetProperty(BindingContext context) { if (context == null) { throw new ArgumentNullException("context"); } T result = this.channelBinding.GetProperty(context.BindingParameters); if (result != default(T)) { return result; } result = this.listenerBinding.GetProperty(context.BindingParameters); if (result != default(T)) { return result; } return context.GetInnerProperty(); } public override string Scheme { get { return listenerBinding.Scheme; } } // one example of how to use CompositeTransportBindingElement static Binding CreateSampleBinding() { CustomBinding oneWayHttpBinding = new CustomBinding( new OneWayBindingElement(), TextMessageEncodingBindingElement(), new HttpTransportBindingElement()); UdpTransportBindingElement udpBE = new UdpTransportBindingElement(); udpBE.Multicast = true; CustomBinding oneWayUdpBinding = new CustomBinding(textBE, udpBE); CompositeTransportBindingElement compositeTransportBE = new CompositeTransportBindingElement( oneWayHttpBinding, oneWayUdpBinding); CompositeDuplexBindingElement compositeBE = new CompositeDuplexBindingElement(); string clientBaseAddress = "http://localhost:8080/Discovery/" + Guid.NewGuid().ToString(); compositeBE.ClientBaseAddress = new Uri(clientBaseAddress); return new CustomBinding(compositeBE, compositeTransportBE); } } }