将自定义httpMessageHandler添加到.net core中的服务注册

问题描述 投票:0回答:1

我有这个注册课程,如你所见:

 public static class InternalSymbolServiceRegister
    {
        public const string DefaultConfigSectionName = "myService";

        public static IServiceCollection AddSymbols(
            this IServiceCollection services, 
            IConfiguration configuration,
            HttpMessageHandler customHttpMessageHandler = null,
            string configSectionName = DefaultConfigSectionName)
        {
            services.Configure<SymbolOptions>(configuration.GetSection(configSectionName));
            services.AddHttpClient<ISymbolService, InternalSymbolService>((sp, client) =>
            {
                var options = sp.GetRequiredService<IOptions<SymbolOptions>>().Value;
                client.BaseAddress = new Uri(options.ServiceUrl);
                client.Timeout = TimeSpan.FromMilliseconds(options.TimeoutMs);
              
            }).AddPolicyHandler(GetCircuitBreakerPolicy());
            if (customHttpMessageHandler != null)
            {
                services.Configure<HttpClientFactoryOptions>(nameof(ISymbolService), options =>
                {
                    options.HttpMessageHandlerBuilderActions.Add(builder =>
                    {
                        builder.PrimaryHandler = customHttpMessageHandler;
                    });
                });
            }

            return services;
        }

        private static IAsyncPolicy<HttpResponseMessage> GetCircuitBreakerPolicy()
            => HttpPolicyExtensions
                .HandleTransientHttpError()
                .CircuitBreakerAsync(handledEventsAllowedBeforeBreaking: 5, durationOfBreak: TimeSpan.FromSeconds(7));

    }

如您所见,我想将我的 httpMessageHandler 注入到我的注册中以添加自定义标头:

if (customHttpMessageHandler != null)
                {
                    services.Configure<HttpClientFactoryOptions>(nameof(ISymbolService), options =>
                    {
                        options.HttpMessageHandlerBuilderActions.Add(builder =>
                        {
                            builder.PrimaryHandler = customHttpMessageHandler;
                        });
                    });
                }

这里我在启动中添加我的符号

services.AddSymbols(Configuration, customHttpMessageHandler: new InternalSymbolHeaderHandler());

这是我的标题:

 public sealed class InternalSymbolHeaderHandler : DelegatingHandler
    {
        protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            request.Headers.Add("admin_header", "_11111");
            return await base.SendAsync(request, cancellationToken);
        }
    }

但是当我调用我的 api 时,客户端的标头是空的:

asp.net-core httpclient inject message-handlers iserviceprovider
1个回答
0
投票

检查示例代码后,我发现你缺少

services.AddTransient<InternalSymbolHeaderHandler>();

// you are missing this line
services.AddTransient<InternalSymbolHeaderHandler>();
services.AddSymbols(Configuration, customHttpMessageHandler: new InternalSymbolHeaderHandler());
© www.soinside.com 2019 - 2024. All rights reserved.