Пример #1
0
        public static async Task <T> PostAsync <T, S>(
            this HttpClient httpClient,
            string serviceName,
            string virtualPath,
            string code = null,
            object body = null,
            IServiceFinder <S> serviceFinder = null,
            ILoadBalancer balancer           = null) where S : IService
        {
            if (serviceName.IsNullOrEmpty())
            {
                throw new AngleExceptions("service name not exits");
            }

            if (virtualPath.IsNullOrEmpty())
            {
                throw new AngleExceptions("virtualPath not exits");
            }

            if (serviceFinder.IsNull())
            {
                throw new AngleExceptions("serviceFinder cannot be null");
            }

            balancer = balancer ?? new WeightRoundBalancer();

            var userServices = await serviceFinder.FindByNameAsync(serviceName);

            var foundedService = default(S);

            if (!userServices.IsNull() && userServices.Count > 0)
            {
                var servicesDictionary = (from us in userServices select new { service = us, weight = us.Weight, }).ToDictionary(x => x.service, y => y.weight);
                foundedService = balancer.Balance(servicesDictionary);
            }

            using (var client = new HttpClient())
            {
                var url = foundedService?.Address.ToString() + virtualPath;

                if (!code.IsNullOrEmpty())
                {
                    client.DefaultRequestHeaders.Add("Authorization", code);
                }

                return(await client.PostAsync <T>(url, body));
            }
        }