第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何在 .NET Core 中使用 HttpClientHandler

如何在 .NET Core 中使用 HttpClientHandler

C#
哈士奇WWW 2021-07-19 16:18:37
我想使用的HttpClientFactory是在.NET 2.1核心可用,但我也想用HttpClientHandler,以利用AutomaticDecompression創(chuàng)建時(shí)屬性HttpClients。我很掙扎,因?yàn)?AddHttpMessageHandler<>需要一個(gè)DelegatingHandler不是HttpClientHandler.有誰(shuí)知道如何讓這個(gè)工作?
查看完整描述

2 回答

?
白板的微信

TA貢獻(xiàn)1883條經(jīng)驗(yàn) 獲得超3個(gè)贊

其實(shí)我沒(méi)有使用自動(dòng)解壓,但實(shí)現(xiàn)這一點(diǎn)的方法是正確注冊(cè)http客戶端


services.AddHttpClient<MyCustomHttpClient>()

   .ConfigureHttpMessageHandlerBuilder((c) =>

     new HttpClientHandler()

     {

        AutomaticDecompression = System.Net.DecompressionMethods.GZip

     }

   )

   .AddHttpMessageHandler((s) => s.GetService<MyCustomDelegatingHandler>())


查看完整回答
反對(duì) 回復(fù) 2021-07-31
?
ITMISS

TA貢獻(xiàn)1871條經(jīng)驗(yàn) 獲得超8個(gè)贊

通過(guò) HttpClientBuilder 的 ConfigurePrimaryHttpMessageHandler() 方法定義主 HttpMessageHandler 更合適。請(qǐng)參閱下面的示例以配置類型化客戶端。


services.AddHttpClient<TypedClient>()

    .ConfigureHttpClient((sp, httpClient) =>

    {

        var options = sp.GetRequiredService<IOptions<SomeOptions>>().Value;

        httpClient.BaseAddress = options.Url;

        httpClient.Timeout = options.RequestTimeout;

    })

    .SetHandlerLifetime(TimeSpan.FromMinutes(5))

    .ConfigurePrimaryHttpMessageHandler(x => new HttpClientHandler() 

    {

        AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

    })

    .AddHttpMessageHandler(sp => sp.GetService<SomeCustomHandler>().CreateAuthHandler())

    .AddPolicyHandlerFromRegistry(PollyPolicyName.HttpRetry)

    .AddPolicyHandlerFromRegistry(PollyPolicyName.HttpCircuitBreaker);

您還可以通過(guò)使用 Polly 庫(kù)的特殊構(gòu)建器方法來(lái)定義錯(cuò)誤處理策略。在這個(gè)示例中,策略應(yīng)該被預(yù)定義并存儲(chǔ)到策略注冊(cè)服務(wù)中。


public static IServiceCollection AddPollyPolicies(

    this IServiceCollection services, 

    Action<PollyPoliciesOptions> setupAction = null)

{

    var policyOptions = new PollyPoliciesOptions();

    setupAction?.Invoke(policyOptions);


    var policyRegistry = services.AddPolicyRegistry();


    policyRegistry.Add(

        PollyPolicyName.HttpRetry,

        HttpPolicyExtensions

            .HandleTransientHttpError()

            .WaitAndRetryAsync(

                policyOptions.HttpRetry.Count,

                retryAttempt => TimeSpan.FromSeconds(Math.Pow(policyOptions.HttpRetry.BackoffPower, retryAttempt))));


    policyRegistry.Add(

        PollyPolicyName.HttpCircuitBreaker,

        HttpPolicyExtensions

            .HandleTransientHttpError()

            .CircuitBreakerAsync(

                handledEventsAllowedBeforeBreaking: policyOptions.HttpCircuitBreaker.ExceptionsAllowedBeforeBreaking,

                    durationOfBreak: policyOptions.HttpCircuitBreaker.DurationOfBreak));


    return services;

}


查看完整回答
反對(duì) 回復(fù) 2021-07-31
  • 2 回答
  • 0 關(guān)注
  • 384 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)