Пример #1
0
        /// <summary>
        /// Call the SensorValues service and show the results in the console window.
        /// </summary>
        /// <param name="endpointAddress">The address the service will be called.</param>
        public static SSimpSample InvokeService(EndpointAddress endpointAddress, bool silent)
        {
            // Create a client (get the settings in the app.config);
            using (SensorValuesClient client =
                       new SensorValuesClient(new InstanceContext(new DiscoveryCallBack()), "SensorValues"))
            {
                // Connect to the discovered service endpoint.
                client.Endpoint.Address = endpointAddress;
                client.Endpoint.Binding.ReceiveTimeout = new TimeSpan(0, 0, 2);

                ////Get the ClientBaseAddress, to change from Localhost to an specific IP Address;
                //CustomBinding binding = (CustomBinding)client.Endpoint.Binding;
                //Uri baseAddress = ((CompositeDuplexBindingElement)binding.Elements[0]).ClientBaseAddress;

                //if (baseAddress != null)
                //{
                //    // Get the Client Machine Name (NB. Use the IP address as can be problems using the name)
                //    string myClientMachineName = System.Net.Dns.GetHostName();
                //    System.Net.IPHostEntry myClientMachineAddressList = System.Net.Dns.GetHostEntry(myClientMachineName);
                //    string myClientMachineIP = myClientMachineName;
                //    foreach (System.Net.IPAddress ip in myClientMachineAddressList.AddressList)
                //    {
                //        // Make sure to get the IPv4 address;
                //        if (!(ip.IsIPv6LinkLocal || ip.IsIPv6Multicast || ip.IsIPv6SiteLocal || ip.IsIPv6Teredo))
                //        {
                //            myClientMachineIP = ip.ToString();
                //            break;
                //        }
                //    }
                //    ((CompositeDuplexBindingElement)binding.Elements[0]).ClientBaseAddress = new Uri(baseAddress.AbsoluteUri.Replace("localhost", myClientMachineIP));
                //}

                // Call the service.
                if (!silent)
                {
                    Console.WriteLine("Invoking at {0}", endpointAddress);
                }

                client.Open();
                SSimpSample response;

                do
                {
                    response = client.GetSensorValues();

                    // Show the results in the console window.
                    if (!silent)
                    {
                        ShowValues(response);
                        Console.WriteLine("Read Again?");
                    }
                } while (!silent && (Console.ReadKey().Key == ConsoleKey.Y));
                // Closing the client gracefully closes the connection and cleans up resources.
                client.Close();

                // Return the Values;
                return(response);
            }
        }
Пример #2
0
        void InvokeService(EndpointAddress endpointAddress)
        {
            InstanceContext ithis            = new InstanceContext(this);
            String          callbackEndpoint = "http://vs2010test/blub";


            // Create a client
            SensorValuesClient client = new SensorValuesClient(new InstanceContext(this));

            client.Endpoint.Address = endpointAddress;
            //    callbackEndpoint=client.InnerChannel.LocalAddress.Uri.AbsoluteUri;
            callbackEndpoint = client.InnerDuplexChannel.LocalAddress.Uri.AbsoluteUri;

            Console.WriteLine("Invoking at {0}", endpointAddress);

            // client.GetSensorValues(out accelleration,out audio,out light,out force,out temperature);
            SSimpSample response = client.GetSensorValues();

            Console.WriteLine("Got response: {0}", response);


            Console.WriteLine("Subscribing event {1} at {0}", endpointAddress, "http://www.teco.edu/SensorValues/SensorValuesEventOut");
            EventSourceClient eventSource = new EventSourceClient(ithis);

            eventSource.Endpoint.Address = endpointAddress;

            SubscribeOpRequest s = new SubscribeOpRequest();

            s.Subscribe = new Subscribe();

            (s.Subscribe.Delivery = new DeliveryType()).Mode = "http://schemas.xmlsoap.org/ws/2004/08/eventing/DeliveryModes/Push";

            XmlDocument doc = new XmlDocument();

            using (XmlWriter writer = doc.CreateNavigator().AppendChild())
            {
                EndpointReferenceType notifyTo = new EndpointReferenceType();

                (notifyTo.Address = new AttributedURI()).Value = callbackEndpoint;
                XmlRootAttribute notifyToElem = new XmlRootAttribute("NotifyTo");
                notifyToElem.Namespace = "http://schemas.xmlsoap.org/ws/2004/08/eventing";

                new XmlSerializer(notifyTo.GetType(), notifyToElem).Serialize(writer, notifyTo);
            }

            (s.Subscribe.Delivery.Any = new XmlElement[1])[0] =
                doc.DocumentElement;

            (s.Subscribe.Filter = new FilterType()).Dialect = "http://schemas.xmlsoap.org/ws/2006/02/devprof/Action";

            (s.Subscribe.Filter.Any = new System.Xml.XmlNode[1]) [0] =
                new System.Xml.XmlDocument().CreateTextNode("http://www.teco.edu/SensorValues/SensorValuesEventOut");

            SubscribeOpResponse subscription;

            try
            {
                Console.WriteLine("Press <ENTER> to subscribe.");
                Console.ReadLine();
                subscription = eventSource.SubscribeOp(s);
            }
            catch (TimeoutException t)
            {
                Console.WriteLine("Error reply time out: {0}!!", t.Message);
                return;
            }
            //  eventSource.Close();


            String subscriptionId = null;


            foreach (XmlNode xel in subscription.SubscribeResponse.SubscriptionManager.ReferenceParameters.Any)
            {
                if (xel.LocalName.Equals("Identifier") && xel.NamespaceURI.Equals("http://schemas.xmlsoap.org/ws/2004/08/eventing"))
                {
                    subscriptionId = xel.InnerText;
                }
            }

            Console.WriteLine("Got subscription: {0}", subscriptionId);

            Console.WriteLine("Press <ENTER> to unsubscribe.");
            Console.ReadLine();

            UnsubscribeOpRequest unsubscribe = new UnsubscribeOpRequest();

            unsubscribe.Identifier = subscriptionId;

            Console.WriteLine("Unsubscribing {0}", subscriptionId);
            SubscriptionManagerClient subscriptionManager = new SubscriptionManagerClient();

            subscriptionManager.Endpoint.Address = endpointAddress;
            subscriptionManager.UnsubscribeOp(unsubscribe);

            //Closing the client gracefully closes the connection and cleans up resources
            //client.Close();
        }