Exemple #1
0
        /// <summary>
        /// 获取一个服务连接,使用方式始下
        /// <example>
        /// <code>
        ///  using (WcfTcpClient<IUserService> Client = WcfClientFactory.CreateClient<IUserService>())
        ///  {
        ///          User user = Client.Channel.GetUser("henry");
        ///  }
        /// </code>
        /// </example>
        /// </summary>
        /// <typeparam name="T">服务接口</typeparam>
        /// <returns>WcfTcpClient<T> </returns>
        public WcfTcpClient <T> CreateClient <T>()
        {
            IWcfTcpClientPool pool = null;

            lock (mClientPools)
            {
                Type type = typeof(T);
                if (!mClientPools.TryGetValue(type, out pool))
                {
                    ServiceContractAttribute[] scs = (ServiceContractAttribute[])type.GetCustomAttributes(typeof(ServiceContractAttribute), false);
                    if (scs == null || scs.Length == 0)
                    {
                        throw CN100WcfException.SERVER_CONTRACT_NOFOUND();
                    }
                    string name;
                    if (string.IsNullOrEmpty(scs[0].Name))
                    {
                        name = type.Name;
                    }
                    else
                    {
                        name = scs[0].Name;
                    }

                    string[] address = new string[Connections.Count];
                    for (int i = 0; i < Connections.Count; i++)
                    {
                        address[i] = string.Format(BASEADDRESS, Connections[i].IPAddress, Connections[i].Port, name);
                    }
                    pool = new WcfTcpClientPool <T>(Binding, DefaultConnections, address);
                    mClientPools.Add(type, pool);
                }
            }
            WcfTcpClient <T> client = (WcfTcpClient <T>)pool.GetClient();

            if (client == null)
            {
                throw new Exception("Connection is not available!");
            }
            TimeSpan dt = DateTime.Now - client.ActiveTime;

            while (dt.TotalMinutes > 5)
            {
                client = (WcfTcpClient <T>)pool.GetClient();
                if (client == null)
                {
                    throw new Exception("Connection is not available!");
                }
                dt = DateTime.Now - client.ActiveTime;
            }

            client.ActiveTime = DateTime.Now;
            return(client);
        }
 public void Initialize(int count)
 {
     for (int i = 0; i < count; i++)
     {
         if (Status == NodeStatus.None)
         {
             WcfTcpClient <T> item = createClient();
             if (item != null)
             {
                 Push(item);
             }
         }
     }
 }
 private void OnVerify()
 {
     try
     {
         WcfTcpClient <T> item = createClient();
         if (item != null)
         {
             Push(item);
         }
     }
     catch (Exception e_)
     {
         WcfClientFactory.Log.Error(e_);
     }
 }
 public void Dispose()
 {
     lock (this)
     {
         if (mIsDisposed)
         {
             lock (mQueue)
             {
                 while (mQueue.Count > 0)
                 {
                     WcfTcpClient <T> item = (WcfTcpClient <T>)mQueue.Dequeue();
                     item.Pool = null;
                 }
             }
         }
     }
 }
 private WcfTcpClient <T> createClient()
 {
     try
     {
         WcfTcpClient <T> result = new WcfTcpClient <T>();
         result.Channel          = mFactory.CreateChannel(new EndpointAddress(mAddress));
         result.Pool             = this;
         result.CObject          = (ICommunicationObject)result.Channel;
         result.CObject.Faulted += result.OnFaulted;
         result.CObject.Open();
         Status = NodeStatus.None;
         return(result);
     }
     catch (Exception e_)
     {
         Status         = NodeStatus.Error;
         mLastErrorTime = Environment.TickCount;
         WcfClientFactory.Log.Error(e_);
         return(null);
     }
 }