Esempio n. 1
0
        static void ServiceFromCode()
        {
            Console.Out.WriteLine("Testing Udp From Code.");

            Binding datagramBinding = new CustomBinding(new BinaryMessageEncodingBindingElement(), new UdpTransportBindingElement());

            // using the 2-way calculator method requires a session since UDP is not inherently request-response
            SampleProfileUdpBinding calculatorBinding = new SampleProfileUdpBinding(true);
            calculatorBinding.ClientBaseAddress = new Uri("soap.udp://localhost:8003/");

            Uri calculatorAddress = new Uri("soap.udp://localhost:8001/");
            Uri datagramAddress = new Uri("soap.udp://localhost:8002/datagram");

            // we need an http base address so that svcutil can access our metadata
            ServiceHost service = new ServiceHost(typeof(CalculatorService), new Uri("http://localhost:8000/udpsample/"));
            ServiceMetadataBehavior metadataBehavior = new ServiceMetadataBehavior();
            metadataBehavior.HttpGetEnabled = true;
            service.Description.Behaviors.Add(metadataBehavior);
            service.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexHttpBinding(), "mex");

            service.AddServiceEndpoint(typeof(ICalculatorContract), calculatorBinding, calculatorAddress);
            service.AddServiceEndpoint(typeof(IDatagramContract), datagramBinding, datagramAddress);
            service.Open();

            Console.WriteLine("Service is started from code...");
            Console.WriteLine("Press <ENTER> to terminate the service and start service from config...");
            Console.ReadLine();

            service.Close();
        }
Esempio n. 2
0
        static void ServiceFromCode()
        {
            Console.Out.WriteLine("Testing Udp From Code.");

            Binding datagramBinding = new CustomBinding(new BinaryMessageEncodingBindingElement(), new UdpTransportBindingElement());

            // using the 2-way calculator method requires a session since UDP is not inherently request-response
            SampleProfileUdpBinding calculatorBinding = new SampleProfileUdpBinding(true);

            calculatorBinding.ClientBaseAddress = new Uri("soap.udp://localhost:8003/");

            Uri calculatorAddress = new Uri("soap.udp://localhost:8001/");
            Uri datagramAddress   = new Uri("soap.udp://localhost:8002/datagram");

            // we need an http base address so that svcutil can access our metadata
            ServiceHost             service          = new ServiceHost(typeof(CalculatorService), new Uri("http://localhost:8000/udpsample/"));
            ServiceMetadataBehavior metadataBehavior = new ServiceMetadataBehavior();

            metadataBehavior.HttpGetEnabled = true;
            service.Description.Behaviors.Add(metadataBehavior);
            service.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexHttpBinding(), "mex");

            service.AddServiceEndpoint(typeof(ICalculatorContract), calculatorBinding, calculatorAddress);
            service.AddServiceEndpoint(typeof(IDatagramContract), datagramBinding, datagramAddress);
            service.Open();

            Console.WriteLine("Service is started from code...");
            Console.WriteLine("Press <ENTER> to terminate the service and start service from config...");
            Console.ReadLine();

            service.Close();
        }
Esempio n. 3
0
        protected override void InitializeFrom(Binding binding)
        {
            base.InitializeFrom(binding);
            SampleProfileUdpBinding udpBinding = (SampleProfileUdpBinding)binding;

            this.OrderedSession           = udpBinding.OrderedSession;
            this.ReliableSessionEnabled   = udpBinding.ReliableSessionEnabled;
            this.SessionInactivityTimeout = udpBinding.SessionInactivityTimeout;
            if (udpBinding.ClientBaseAddress != null)
            {
                this.ClientBaseAddress = udpBinding.ClientBaseAddress;
            }
        }
        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 (SampleProfileUdpBinding.TryCreate(bindingElements, out binding))
                    {
                        binding.Name             = context.Endpoint.Binding.Name;
                        binding.Namespace        = context.Endpoint.Binding.Namespace;
                        context.Endpoint.Binding = binding;
                    }
                }
            }
        }
Esempio n. 5
0
        protected override void OnApplyConfiguration(Binding binding)
        {
            if (binding == null)
            {
                throw new ArgumentNullException("binding");
            }

            if (binding.GetType() != typeof(SampleProfileUdpBinding))
            {
                throw new ArgumentException(string.Format(CultureInfo.CurrentCulture,
                                                          "Invalid type for binding. Expected type: {0}. Type passed in: {1}.",
                                                          typeof(SampleProfileUdpBinding).AssemblyQualifiedName,
                                                          binding.GetType().AssemblyQualifiedName));
            }
            SampleProfileUdpBinding udpBinding = (SampleProfileUdpBinding)binding;

            udpBinding.OrderedSession           = this.OrderedSession;
            udpBinding.ReliableSessionEnabled   = this.ReliableSessionEnabled;
            udpBinding.SessionInactivityTimeout = this.SessionInactivityTimeout;
            if (this.ClientBaseAddress != null)
            {
                udpBinding.ClientBaseAddress = ClientBaseAddress;
            }
        }
        //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;
            }

            SampleProfileUdpBinding sampleProfileUdpBinding = new SampleProfileUdpBinding();
            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);
            }

            SampleProfileUdpBinding sampleProfileUdpBinding = new SampleProfileUdpBinding();

            sampleProfileUdpBinding.InitializeFrom(udpTransportBindingElement, textMessageEncodingBindingElement,
                                                   reliableSessionBindingElement, compositeDuplexBindingElement);
            if (!sampleProfileUdpBinding.IsBindingElementsMatch(udpTransportBindingElement, textMessageEncodingBindingElement,
                                                                reliableSessionBindingElement, compositeDuplexBindingElement))
            {
                return(false);
            }

            binding = sampleProfileUdpBinding;
            return(true);
        }