Example #1
0
        /// <summary>
        /// Connect to WCF service
        /// </summary>
        private void Connect()
        {
            var localSettings = ConnectionSettings.Load();
            //Create a NetTcp binding to the service and set some appropriate timeouts.
            //Use reliable connection so we know when we have been disconnected
            var plexServiceBinding = new NetTcpBinding
            {
                OpenTimeout     = TimeSpan.FromSeconds(2),
                CloseTimeout    = TimeSpan.FromSeconds(2),
                SendTimeout     = TimeSpan.FromSeconds(2),
                ReliableSession =
                {
                    Enabled           = true,
                    InactivityTimeout = TimeSpan.FromMinutes(1)
                }
            };

            //Generate the endpoint from the local settings
            var plexServiceEndpoint = new EndpointAddress(localSettings.getServiceAddress());

            TrayCallback callback = new TrayCallback();

            callback.StateChange += Callback_StateChange;
            var client = new TrayInteractionClient(callback, plexServiceBinding, plexServiceEndpoint);

            //Make a channel factory so we can create the link to the service
            //var plexServiceChannelFactory = new ChannelFactory<PlexServiceCommon.Interface.ITrayInteraction>(plexServiceBinding, plexServiceEndpoint);

            _plexService = null;

            try
            {
                _plexService = client.ChannelFactory.CreateChannel(); //plexServiceChannelFactory.CreateChannel();
                _plexService.Subscribe();
                //If we lose connection to the service, set the object to null so we will know to reconnect the next time the tray icon is clicked
                ((ICommunicationObject)_plexService).Faulted += (s, e) => _plexService = null;
                ((ICommunicationObject)_plexService).Closed  += (s, e) => _plexService = null;
            }
            catch
            {
                _plexService = null;
            }
        }
        /// <summary>
        /// Connect to WCF service
        /// </summary>
        private void Connect()
        {
            //Create a NetTcp binding to the service and set some appropriate timeouts.
            //Use reliable connection so we know when we have been disconnected
            var plexServiceBinding = new NetTcpBinding();

            plexServiceBinding.OpenTimeout                       = _timeOut;
            plexServiceBinding.CloseTimeout                      = _timeOut;
            plexServiceBinding.SendTimeout                       = _timeOut;
            plexServiceBinding.ReliableSession.Enabled           = true;
            plexServiceBinding.ReliableSession.InactivityTimeout = TimeSpan.FromMinutes(1);
            //Generate the endpoint from the local settings
            var plexServiceEndpoint = new EndpointAddress(_address);

            TrayCallback callback = new TrayCallback();

            callback.Stopped += (s, e) => _stopped.Set();
            var client = new TrayInteractionClient(callback, plexServiceBinding, plexServiceEndpoint);

            //Make a channel factory so we can create the link to the service
            //var plexServiceChannelFactory = new ChannelFactory<PlexServiceCommon.Interface.ITrayInteraction>(plexServiceBinding, plexServiceEndpoint);

            _plexService = null;

            try
            {
                _plexService = client.ChannelFactory.CreateChannel();

                _plexService.Subscribe();
                //If we lose connection to the service, set the object to null so we will know to reconnect the next time the tray icon is clicked
                ((ICommunicationObject)_plexService).Faulted += (s, e) => _plexService = null;
                ((ICommunicationObject)_plexService).Closed  += (s, e) => _plexService = null;
            }
            catch
            {
                if (_plexService != null)
                {
                    _plexService = null;
                }
            }
        }