示例#1
0
        public FabioMessageHandler(FabioOptions options, string serviceName = null)
        {
            if (string.IsNullOrWhiteSpace(options.Url))
            {
                throw new InvalidOperationException("Fabio URL was not provided.");
            }

            _options     = options;
            _servicePath = string.IsNullOrWhiteSpace(serviceName) ? string.Empty : $"{serviceName}/";
        }
示例#2
0
        private static IServiceCollection AddFabio(this IServiceCollection services, FabioOptions fabioOptions,
                                                   HttpClientOptions httpClientOptions, Action <IServiceCollection> registerConsul)
        {
            registerConsul(services);
            services.AddSingleton(fabioOptions);

            if (!fabioOptions.Enabled)
            {
                return(services);
            }
            //here if we set Type in HttpClientOption in our appsettings to 'fabio' we will resolve a custom HttpClient for our FabioHttpClient that will resolve using IHttpClient
            //that is a reason to create abstraction on HttpClient - changing 'type' property will switch implementation injected to our `IHttpClient`
            if (httpClientOptions.Type?.ToLowerInvariant() == "fabio")
            {
                //before send a request to particular service we could override original request uri to send request to fabio endpoint
                services.AddTransient <FabioMessageHandler>();
                services.AddHttpClient <IFabioHttpClient, FabioHttpClient>("fabio-http")
                .AddHttpMessageHandler <FabioMessageHandler>();

                //HttpClient issue: https://github.com/aspnet/AspNetCore/issues/13346
                services.RemoveHttpClient();
                services.AddHttpClient <IHttpClient, FabioHttpClient>("fabio")
                .AddHttpMessageHandler <FabioMessageHandler>();
            }

            using var serviceProvider = services.BuildServiceProvider();
            var registration = serviceProvider.GetService <ServiceRegistration>();
            var tags         = GetFabioTags(registration.Name, fabioOptions.Service);

            if (registration.Tags is null)
            {
                registration.Tags = tags;
            }
            else
            {
                registration.Tags.AddRange(tags);
            }
            //update consul registry with our tags for services and this ServiceRegistration information will use by ConsulHostedService of our consul and when app is started it will register our service to consul registry
            services.UpdateConsulRegistration(registration);

            return(services);
        }
示例#3
0
 public static IServiceCollection AddFabio(this IServiceCollection services, FabioOptions fabioOptions,
                                           ConsulOptions consulOptions, HttpClientOptions httpClientOptions)
 => services.AddFabio(fabioOptions, httpClientOptions, b => b.AddConsul(consulOptions, httpClientOptions));