Beispiel #1
0
        public ChannelFactory <TServiceType> GetChannelFactory <TServiceType>(WcfClientChannelConnectionSettingsList objChannelConnections)
            where TServiceType : class
        {
            ChannelFactory <TServiceType> objChannelFactory = null;

            objChannelConnections.Sort();
            foreach (WcfClientChannelConnectionSettings objChannelConnection in objChannelConnections)
            {
                string strKey = objChannelConnection.EndpointAddress.Address;
                if (_objChannelFactoryCache.ContainsKey(strKey) == true)
                {
                    objChannelFactory = (ChannelFactory <TServiceType>)_objChannelFactoryCache[strKey];
                    if ((objChannelFactory == null) || (objChannelFactory.State == CommunicationState.Faulted) || (objChannelFactory.State == CommunicationState.Closed) || (objChannelFactory.State == CommunicationState.Closing))
                    {
                        if (objChannelFactory != null)
                        {
                            objChannelFactory.Abort();
                            objChannelFactory.Close();
                            objChannelFactory.Faulted -= new EventHandler(ChannelFactory_Faulted);
                        }

                        RemoveFromCache(strKey);
                        objChannelFactory = null;
                    }
                    else
                    {
                        break;
                    }
                }

                if (objChannelConnection.IsChannelAvaliable() == true)
                {
                    try
                    {
                        objChannelFactory          = new ChannelFactory <TServiceType>(objChannelConnection.Binding.Binding, objChannelConnection.EndpointAddress.Address);
                        objChannelFactory.Faulted += new EventHandler(ChannelFactory_Faulted);
                        objChannelFactory.Open();

                        AddToCache(strKey, objChannelFactory);
                        break;
                    }
                    catch (CommunicationException)
                    {
                        /// Try to connect to the next channel
                        ///
                    }
                }
            }
            if (objChannelFactory == null)
            {
                throw new Exception(string.Format("There is no channel available for {0}.", typeof(TServiceType)));
            }

            return(objChannelFactory);
        }
Beispiel #2
0
        public void Dispose()
        {
            if (_blnDisposed == false)
            {
                lock (_objSyncObject)
                {
                    _objChannelFactory            = null;
                    _objChannelConnectionSettings = null;
                }

                _blnDisposed = true;
            }
        }
Beispiel #3
0
 public WcfChannelFactory(WcfClientChannelConnectionSettingsList objChannelConnectionSettings)
 {
     ChannelConnectionSettings = objChannelConnectionSettings;
     ChannelFactory            = null;
 }