示例#1
0
        /// <summary>
        /// Control with Callback, connects to the end point address and waits for notifications.
        /// See also http://www.codeproject.com/Articles/39143/C-WCF-Client-Server-without-HTTP-with-Callbacks-Ma
        /// </summary>
        /// <param name="endPointAddress">Server end point address.</param>
        /// <param name="adr">Zone Address to monitor.</param>
        private static void ControlWithCallback(EndpointAddress endPointAddress, Address adr)
        {
            Console.WriteLine(">>> Setup M&C server ...");

            ILog _log = LogManager.GetCurrentClassLogger();
            IMonitorAndControl             pipeProxy      = null;
            IMonitorAndControlNotification serverCallback = new ServerCallback();

            try
            {
                int port    = FindPort();
                var binding = new WSDualHttpBinding("WSDualHttpBinding_IMonitorAndControl");
                binding.ClientBaseAddress = new Uri("http://" + NetworkHelper.getHostName() + ":" + port + "/");

                /*note the "DuplexChannelFactory".  This is necessary for Callbacks.
                 * A regular "ChannelFactory" won't work with callbacks.*/
                DuplexChannelFactory <IMonitorAndControl> pipeFactory =
                    new DuplexChannelFactory <IMonitorAndControl>(
                        new InstanceContext(serverCallback), binding, endPointAddress);
                try
                {
                    Console.WriteLine(">>>   creating channel to {0} with callback address {1}", endPointAddress.Uri.ToString(), binding.ClientBaseAddress.ToString());

                    //Open the channel to the server
                    pipeProxy = pipeFactory.CreateChannel();
                    pipeProxy.Connect();

                    // Get zone status
                    pipeProxy.Monitor(adr);

                    // Listen to changes ...
                    int iSecTimeout = 60;
                    Console.WriteLine(">>>   Wait {0} seconds, and listen to notifications!", iSecTimeout);
                    System.Threading.Thread.Sleep(iSecTimeout * 1000);
                    Console.WriteLine(">>>   Stop listening ....");
                    pipeProxy.RemoveMonitor(adr);
                }
                catch (Exception e)
                {
                    Console.WriteLine("ControlWithCallback - Exception: {0}", e.Message);
                    _log.Fatal(m => m("ControlWithCallback - Exception: {0}", e.Message));
                }
            }
            catch (FaultException <ArgumentException> exc)
            {
                Console.WriteLine("ControlWithCallback - FaultException: {0}", exc);
                _log.Fatal(m => m("ControlWithCallback - FaultException: {0}", exc));
            }
            catch (Exception exc)
            {
                Console.WriteLine("ControlWithCallback - Exception: {0}", exc);
                _log.Fatal(m => m("ControlWithCallback - Exception: {0}", exc));
            }
        }
        /// <summary>
        /// Monitors a zone. (Subscribes for zone state changes.)
        /// </summary>
        /// <param name="zoneId">Address of the zone.</param>
        /// <param name="subscriber">The subscriber.</param>
        public void Monitor(Address zoneId, ZoneNotification subscriber)
        {
            _log.Trace(m => m(String.Format("M&C Proxy; Monitor(); Address: {0}", zoneId)));

            try
            {
                if (_zoneSubscriptions.ContainsKey(zoneId) == false)
                {
                    _zoneSubscriptions.Add(zoneId, new ZoneSubscriptions(zoneId));
                    _mcServiceProxy.Monitor(zoneId);
                }
                _zoneSubscriptions[zoneId].AddSubscriber(subscriber);
            }
            catch (ArgumentException exc)
            {
                _log.Error("Failed to subscribe for the zone.", exc);
                throw exc;
            }
        }