Пример #1
0
        public static void RegisterServiceForwarder <T>(this IServiceCollection services, string serviceName) where T : class
        {
            var             clientName = typeof(T).ToString();
            RestEaseOptions options    = ConfigureRestEaseOptions(services);

            ConfigureDefaultClient(services, clientName, serviceName, options);
            ConfigureForwarder <T>(services, clientName);
        }
Пример #2
0
        private static RestEaseOptions ConfigureRestEaseOptions(IServiceCollection services)
        {
            IConfiguration configuration;

            using (var serviceProvider = services.BuildServiceProvider())
            {
                configuration = serviceProvider.GetService <IConfiguration>();
            }

            services.Configure <RestEaseOptions>(configuration.GetSection("restEase"));

            RestEaseOptions options = new RestEaseOptions();

            configuration.GetSection("restEase").Bind(options);
            return(options);
        }
Пример #3
0
        private static void ConfigureDefaultClient(IServiceCollection services, string clientName,
                                                   string serviceName, RestEaseOptions options)
        {
            services.AddHttpClient(clientName, client =>
            {
                var service = options.Services.SingleOrDefault(s => s.Name.Equals(serviceName,
                                                                                  StringComparison.InvariantCultureIgnoreCase));
                if (service == null)
                {
                    throw new Exception($"RestEase service: '{serviceName}' was not found.");
                }

                client.BaseAddress = new UriBuilder
                {
                    Scheme = service.Scheme,
                    Host   = service.Host,
                    Port   = service.Port
                }.Uri;
            });
        }