public async Task <HttpResponseMessage> SendRequest(HttpRequestMessage request)
        {
            EndpointOption      endpoint = SelectEndpointForRequest(request);
            string              cacheKey = CalculateCacheKeyForEndpoint(endpoint);
            HttpClient          client   = GetOrCreateHttpClient(endpoint, cacheKey);
            HttpResponseMessage response = await client.SendAsync(request);

            return(response);
        }
예제 #2
0
        public void RegisterTransitionAction(string transitionKey, EndpointOption option, params Action[] actions)
        {
            var found = Machine.States.FirstOrDefault(x => x.Name.Equals(transitionKey, StringComparison.OrdinalIgnoreCase));

            if (found != null)
            {
                if (found.EndpointActions == null)
                {
                    found.EndpointActions = new List <IEndpointAction>();
                }
                found.EndpointActions.Add(new EndpointAction(option, actions));
            }
        }
        private HttpClient GetOrCreateHttpClient(EndpointOption endpoint, string cacheKey)
        {
            var cachedHttpClient = cache.GetOrCreate <HttpClient>(cacheKey, entry => {
                //Questa entry della cache verrà invalidata al ricaricamento della configurazione
                entry.AddExpirationToken(configuration.GetReloadToken());
                //Mi sottoscrivo alla rimozione dalla cache, così che possa fare il dispose
                entry.PostEvictionCallbacks.Add(GetPostEvictionCallbackRegistration());

                //Creo l'istanza che verrà messa in cache
                HttpClient client = CreateHttpClient(endpoint);
                return(client);
            });

            return(cachedHttpClient);
        }
        private HttpClient CreateHttpClient(EndpointOption endpoint)
        {
            var handler = new HttpClientHandler();

            if (!string.IsNullOrEmpty(endpoint.Proxy))
            {
                handler.Proxy    = new WebProxy(endpoint.Proxy);
                handler.UseProxy = true;
            }
            //TODO: dalle opzioni potrebbero arrivare anche autenticazione, headers, ecc.

            var client = new HttpClient(handler, disposeHandler: true);

            logger.LogInformation($"Created a new instance of HttpClient for endpoint '{endpoint.Name}'");
            return(client);
        }
 private string CalculateCacheKeyForEndpoint(EndpointOption endpoint)
 {
     return($"endpoint-{endpoint.Selector}");
 }
예제 #6
0
 public EndpointAction(EndpointOption endpointOption, params Action[] actions)
 {
     _EndpointOption = endpointOption;
     _Actions        = new List <Action>(actions);
 }