Exemplo n.º 1
0
        /// <summary>
        /// Create or reuse HttpClientUser for userId
        /// </summary>
        /// <param name="userId">id of user</param>
        /// <returns>HttpClientUser</returns>
        private HttpClientUser CreateOrReuseClient(string userId)
        {
            HttpClientUser client = null;

            client = _clients.Where(c => c != null && c.UserId == userId).FirstOrDefault();
            if (client == null)
            {
                client = _clients.Where(c => c != null && c.LockCount == 0).FirstOrDefault();
                if (client != null)
                {
                    if (client.HttpClient != null) // Must be diposed
                    {
                        client.HttpClient.Dispose();
                        client.HttpClient = null;
                    }
                }
                else
                {
                    for (int i = 0; i < _clients.Length; i++)
                    {
                        if (_clients[i] == null)
                        {
                            _clients[i] = client = new HttpClientUser()
                            {
                                UserId = userId
                            };
                        }
                    }
                }
            }
            if (client == null)
            {
                _logger.LogCritical("Unable to create HttpClient in HttpClientPool, max client : " + _maxPoolSize);
            }
            return(client);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Create or Reuse HttpClient
        /// </summary>
        /// <param name="client">HttpClientUser</param>
        /// <param name="userIdentityCookie">user Cookie on server</param>
        private void CreateOrReuseHttpClient(HttpClientUser client, Cookie userIdentityCookie)
        {
            if (client.HttpClient != null && client.UserIdentityCookie != userIdentityCookie.Value)
            {
                client.UserIdentityCookie = null;
                client.HttpClient.Dispose();
                client.HttpClient = null;
            }

            if (client.HttpClient == null) // Get session cookie for call web api service
            {
                var cookieContainer = new CookieContainer();
                var handler         = new HttpClientHandler()
                {
                    CookieContainer = cookieContainer
                };
                cookieContainer.Add(_baseAdress, new Cookie(userIdentityCookie.Name, userIdentityCookie.Value));
                client.UserIdentityCookie = userIdentityCookie.Value;
                client.HttpClient         = new HttpClient(handler)
                {
                    BaseAddress = _baseAdress
                };
            }
        }