Exemplo n.º 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;
            }
        }
        public 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 {
                OpenTimeout            = TimeSpan.FromMilliseconds(500),
                CloseTimeout           = TimeSpan.FromMilliseconds(500),
                SendTimeout            = TimeSpan.FromMilliseconds(500),
                MaxReceivedMessageSize = 2147483647,
                MaxBufferSize          = 2147483647,
                MaxBufferPoolSize      = 2147483647,
                ReliableSession        =
                {
                    Enabled           = true,
                    InactivityTimeout = TimeSpan.FromMinutes(1)
                }
            };
            //Generate the endpoint from the local settings
            var plexServiceEndpoint = new EndpointAddress(_traySettings.GetServiceAddress());
            var callback            = new TrayCallback();

            callback.StateChange   += Callback_StateChange;
            callback.SettingChange += Callback_SettingChange;
            var client = new TrayInteractionClient(callback, 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
                _plexService.Faulted += (_, _) => _plexService = null;
                _plexService.Closed  += (_, _) => _plexService = null;
            }
            catch (Exception e)
            {
                Logger("Exception connecting: " + e.Message, LogEventLevel.Warning);
                _plexService = null;
            }
        }