Beispiel #1
0
        public static IHttpClientBuilder AddFluentHttpClient(this IServiceCollection sc,
                                                             FluentHttpClientSetupOptions options = null)
        {
            options = options ?? new FluentHttpClientSetupOptions();

            sc.AddLogging();
            sc.TryAddTransient <HttpRetryHandler>();
            sc.TryAddTransient <HttpTimeoutHandler>();
            sc.TryAddTransient <IFluentHttpClient, FluentHttp>();
            sc.TryAddTransient <ITypedHttpClient, TypedHttpClient>();



            if (options.UseDefaultSerializer)
            {
                sc.AddSingleton(options.HttpContentSerializerSettings ?? new HttpContentSerializerSettings());
                sc.TryAddEnumerable(ServiceDescriptor.Singleton <IHttpContentSerializer, HttpContentJsonSerializer>());
            }

            sc.TryAddTransient(x => x.GetService <IHttpClientFactory>().CreateClient(defaultHttpClientName));

            sc.TryAddTransient <IHttpClientWrapper, HttpClientWrapper>();
            sc.TryAddTransient <HttpClientRequestSender>();

            return(sc.AddHttpClient(defaultHttpClientName));
        }
Beispiel #2
0
        /// <summary>
        /// Add default handlers to your httpclient setup.
        /// </summary>
        /// <param name="builder"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public static IHttpClientBuilder AddDefaultHttpHandlers(this IHttpClientBuilder builder, FluentHttpClientSetupOptions options = null)
        {
            options = options ?? new FluentHttpClientSetupOptions();

            var result = builder
                         .AddHttpMessageHandler <HttpRetryHandler>()
                         .AddHttpMessageHandler <HttpTimeoutHandler>();

            return(result);
        }
Beispiel #3
0
        public static IHttpClientBuilder AddFluentHttpClient(this IServiceCollection sc, FluentHttpClientSetupOptions options = null)
        {
            options = options ?? new FluentHttpClientSetupOptions();

            sc.TryAddEnumerable(ServiceDescriptor.Singleton <IHttpSerializer, HttpRequestJsonSerializer>());
            sc.TryAddTransient <IHttpRequestSender>(sp => new HttpRequestSender(sp.GetService <IHttpClientFactory>().CreateClient(options.Name)));
            sc.TryAddTransient <IFluentHttpClient, DefaultHttpClient>();

            var builder = sc.AddHttpClient(options.Name)
                          .ConfigurePrimaryHttpMessageHandler(config => new HttpClientHandler
            {
                AllowAutoRedirect       = false,
                UseCookies              = false,
                AutomaticDecompression  = System.Net.DecompressionMethods.Deflate | System.Net.DecompressionMethods.GZip,
                MaxConnectionsPerServer = 1024,
            })
                          .AddHttpMessageHandler(() => new HttpClientRetryHandler())
                          .AddHttpMessageHandler(() => new HttpClientTimedoutHandler());

            if (options.EnablePerformanceLog)
            {
                builder.AddHttpMessageHandler((sp) => new HttpClientPerfLogHandler());
            }

            return(builder);
        }