Exemple #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));
            }
        }
Exemple #2
0
        /// <summary>
        /// Get zone status; connects to the end point address and reads the zone status.
        /// </summary>
        /// <param name="endPointAddress">Server end point address.</param>
        /// <param name="adr">Zone Address to get the status.</param>
        private static void GetZoneState(EndpointAddress endPointAddress, Address adr)
        {
            Console.WriteLine(">>> Setup M&C server ...");

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

            try
            {
                //var binding = new WSDualHttpBinding(WSDualHttpSecurityMode.None);
                var binding = new WSDualHttpBinding("WSDualHttpBinding_IMonitorAndControl");

                int port = FindPort();
                //binding.ClientBaseAddress = new Uri("http://" + machineIpOrName + ":" + port + "/");
                //binding.ClientBaseAddress = new Uri("http://" + "192.168.1.115" + ":" + port + "/");
                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 /*new NetTcpBinding()*/,
                        endPointAddress /*new EndpointAddress("net.tcp://localhost:8000/ISubscribe")*/);
                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
                    ZoneState state = pipeProxy.GetZoneState(adr);
                    Console.WriteLine(">>>   zone state: {0}", state.ToString());
                }
                catch (Exception e)
                {
                    Console.WriteLine("GetZoneState - Exception: {0}", e.Message);
                    _log.Fatal(m => m("GetZoneState - Exception: {0}", e.Message));
                }
            }
            catch (FaultException <ArgumentException> exc)
            {
                Console.WriteLine("GetZoneState - FaultException: {0}", exc);
                _log.Fatal(m => m("GetZoneState - FaultException: {0}", exc));
            }
            catch (Exception exc)
            {
                Console.WriteLine("GetZoneState - Exception: {0}", exc);
                _log.Fatal(m => m("GetZoneState - Exception: {0}", exc));
            }
        }
        private void Initialize(EndpointAddress endPointAddress, string clientIpOrName)
        {
            try
            {
                _log.Trace(m => m("M&C Proxy; Initialize( {0} )", endPointAddress.Uri.ToString()));

                _mcServiceProxy = CreateMCClient(endPointAddress, clientIpOrName);
                _mcServiceProxy.Connect();

                _timerRenewLease = new Timer(OnRenewLeaseCallback);
                _timerRenewLease.Change(RENEW_LEASE_TIME, Timeout.Infinite);

                _log.Trace(m => m("M&C Proxy; Initialize() done."));
            }
            catch (Exception exc)
            {
                _log.Error("Creating connection to the service failed.", exc);
                (_mcServiceProxy as MonitorAndControlClient).Abort();
            }
        }