Exemplo n.º 1
0
        /// <summary>
        /// This is a custom method to register the HttpClient and typed factory. Needed because we need to access the config name when creating the typed client
        /// </summary>
        private static IHttpClientBuilder AddGrpcHttpClient <TClient>(this IServiceCollection services, string name)
            where TClient : class
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            services
            .AddHttpClient(name)
            .ConfigurePrimaryHttpMessageHandler(() =>
            {
                // Set PrimaryHandler to null so we can track whether the user
                // set a value or not. If they didn't set their own handler then
                // one will be created by PostConfigure.
                return(null);
            });

            services.PostConfigure <HttpClientFactoryOptions>(name, options =>
            {
                options.HttpMessageHandlerBuilderActions.Add(static builder =>
                {
                    if (builder.PrimaryHandler == null)
                    {
                        // This will throw in .NET Standard 2.0 with a prompt that a user must set a handler.
                        // Because it throws it should only be called in PostConfigure if no handler has been set.
                        var handler = HttpHandlerFactory.CreatePrimaryHandler();
#if NET5_0
                        handler = HttpHandlerFactory.EnsureTelemetryHandler(handler);
#endif

                        builder.PrimaryHandler = handler;
                    }
                });
            });
        /// <summary>
        /// This is a custom method to register the HttpClient and typed factory. Needed because we need to access the config name when creating the typed client
        /// </summary>
        private static IHttpClientBuilder AddGrpcHttpClient <TClient>(this IServiceCollection services, string name)
            where TClient : class
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            services
            .AddHttpClient(name)
            .ConfigurePrimaryHttpMessageHandler(() =>
            {
                var handler = HttpHandlerFactory.CreatePrimaryHandler();
#if NET5_0
                handler = HttpHandlerFactory.EnsureTelemetryHandler(handler);
#endif
                return(handler);
            });

            var builder = new DefaultHttpClientBuilder(services, name);

            builder.Services.AddTransient <TClient>(s =>
            {
                var clientFactory = s.GetRequiredService <GrpcClientFactory>();
                return(clientFactory.CreateClient <TClient>(builder.Name));
            });

            ReserveClient(builder, typeof(TClient), name);

            return(builder);
        }
Exemplo n.º 3
0
        private static HttpMessageInvoker CreateInternalHttpInvoker(HttpMessageHandler?handler)
        {
            // HttpMessageInvoker should always dispose handler if Disposed is called on it.
            // Decision to dispose invoker is controlled by _shouldDisposeHttpClient.
            if (handler == null)
            {
                handler = HttpHandlerFactory.CreatePrimaryHandler();
            }

#if NET5_0
            handler = HttpHandlerFactory.EnsureTelemetryHandler(handler);
#endif

            // Use HttpMessageInvoker instead of HttpClient because it is faster
            // and we don't need client's features.
            var httpInvoker = new HttpMessageInvoker(handler, disposeHandler: true);

            return(httpInvoker);
        }