public void ImportEndpoint(WsdlImporter importer, WsdlEndpointConversionContext context) { if (context == null) { throw new ArgumentNullException("context"); } if (context.Endpoint.Binding == null) { throw new ArgumentNullException("context.Endpoint.Binding"); } BindingElementCollection bindingElements = context.Endpoint.Binding.CreateBindingElements(); TransportBindingElement transportBindingElement = bindingElements.Find <TransportBindingElement>(); if (transportBindingElement is UdpTransportBindingElement) { ImportAddress(context); } if (context.Endpoint.Binding is CustomBinding) { Binding binding; if (transportBindingElement is UdpTransportBindingElement) { //if TryCreate is true, the CustomBinding will be replace by a SampleProfileUdpBinding in the //generated config file for better typed generation. if (NetUdpBinding.TryCreate(bindingElements, out binding)) { binding.Name = context.Endpoint.Binding.Name; binding.Namespace = context.Endpoint.Binding.Namespace; context.Endpoint.Binding = binding; } } } }
//try to create a SampleProfileUdpBinding from the collection of BindingElement //returns true if it is possible, with the resulting binding. public static bool TryCreate(BindingElementCollection elements, out Binding binding) { binding = null; if (elements.Count > 4) { return false; } ReliableSessionBindingElement reliableSessionBindingElement = null; CompositeDuplexBindingElement compositeDuplexBindingElement = null; TextMessageEncodingBindingElement textMessageEncodingBindingElement = null; UdpTransportBindingElement udpTransportBindingElement = null; foreach (BindingElement element in elements) { if (element is CompositeDuplexBindingElement) { compositeDuplexBindingElement = element as CompositeDuplexBindingElement; } else if (element is TransportBindingElement) { udpTransportBindingElement = element as UdpTransportBindingElement; } else if (element is TextMessageEncodingBindingElement) { textMessageEncodingBindingElement = element as TextMessageEncodingBindingElement; } else if (element is ReliableSessionBindingElement) { reliableSessionBindingElement = element as ReliableSessionBindingElement; } else { return false; } } if (udpTransportBindingElement == null) { return false; } if (textMessageEncodingBindingElement == null) { return false; } if (((reliableSessionBindingElement != null) && (compositeDuplexBindingElement == null)) || ((reliableSessionBindingElement == null) && (compositeDuplexBindingElement != null))) { return false; } NetUdpBinding sampleProfileUdpBinding = new NetUdpBinding(); sampleProfileUdpBinding.InitializeFrom(udpTransportBindingElement, textMessageEncodingBindingElement, reliableSessionBindingElement, compositeDuplexBindingElement); if (!sampleProfileUdpBinding.IsBindingElementsMatch(udpTransportBindingElement, textMessageEncodingBindingElement, reliableSessionBindingElement, compositeDuplexBindingElement)) { return false; } binding = sampleProfileUdpBinding; return true; }
//try to create a SampleProfileUdpBinding from the collection of BindingElement //returns true if it is possible, with the resulting binding. public static bool TryCreate(BindingElementCollection elements, out Binding binding) { binding = null; if (elements.Count > 4) { return(false); } ReliableSessionBindingElement reliableSessionBindingElement = null; CompositeDuplexBindingElement compositeDuplexBindingElement = null; TextMessageEncodingBindingElement textMessageEncodingBindingElement = null; UdpTransportBindingElement udpTransportBindingElement = null; foreach (BindingElement element in elements) { if (element is CompositeDuplexBindingElement) { compositeDuplexBindingElement = element as CompositeDuplexBindingElement; } else if (element is TransportBindingElement) { udpTransportBindingElement = element as UdpTransportBindingElement; } else if (element is TextMessageEncodingBindingElement) { textMessageEncodingBindingElement = element as TextMessageEncodingBindingElement; } else if (element is ReliableSessionBindingElement) { reliableSessionBindingElement = element as ReliableSessionBindingElement; } else { return(false); } } if (udpTransportBindingElement == null) { return(false); } if (textMessageEncodingBindingElement == null) { return(false); } if (((reliableSessionBindingElement != null) && (compositeDuplexBindingElement == null)) || ((reliableSessionBindingElement == null) && (compositeDuplexBindingElement != null))) { return(false); } NetUdpBinding sampleProfileUdpBinding = new NetUdpBinding(); sampleProfileUdpBinding.InitializeFrom(udpTransportBindingElement, textMessageEncodingBindingElement, reliableSessionBindingElement, compositeDuplexBindingElement); if (!sampleProfileUdpBinding.IsBindingElementsMatch(udpTransportBindingElement, textMessageEncodingBindingElement, reliableSessionBindingElement, compositeDuplexBindingElement)) { return(false); } binding = sampleProfileUdpBinding; return(true); }