dotnet-httpclient

adding a handler to all clients created via IHttpClientFactory?

天大地大妈咪最大 提交于 2020-08-19 12:07:05
问题 Is there a way to add a handler to all clients created by the IHttpClientFactory? I know you can do the following on named clients: services.AddHttpClient("named", c => { c.BaseAddress = new Uri("TODO"); c.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); c.DefaultRequestHeaders.CacheControl = new CacheControlHeaderValue { NoCache = true, NoStore = true, MaxAge = new TimeSpan(0), MustRevalidate = true }; }).ConfigurePrimaryHttpMessageHandler(() => new

adding a handler to all clients created via IHttpClientFactory?

谁说胖子不能爱 提交于 2020-08-19 11:59:50
问题 Is there a way to add a handler to all clients created by the IHttpClientFactory? I know you can do the following on named clients: services.AddHttpClient("named", c => { c.BaseAddress = new Uri("TODO"); c.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); c.DefaultRequestHeaders.CacheControl = new CacheControlHeaderValue { NoCache = true, NoStore = true, MaxAge = new TimeSpan(0), MustRevalidate = true }; }).ConfigurePrimaryHttpMessageHandler(() => new

Dependency injection: HttpClient or HttpClientFactory?

Deadly 提交于 2020-08-02 07:36:46
问题 Everywhere I can see three main approaches to create clients (basic, named, typed) in DI, but I have found nowhere if to inject IHttpClientFactory or HttpClient (both possible). Q1 : What is the difference between injecting IHttpClientFactory or HttpClient please? Q2 : And if IHttpClientFactory is injected, should I use factory.CreateClient() for each call? 回答1: Summary HttpClient can only be injected inside Typed clients for other usages, you need IHttpClientFactory In both scenarios, the

Dependency injection: HttpClient or HttpClientFactory?

拈花ヽ惹草 提交于 2020-08-02 07:36:31
问题 Everywhere I can see three main approaches to create clients (basic, named, typed) in DI, but I have found nowhere if to inject IHttpClientFactory or HttpClient (both possible). Q1 : What is the difference between injecting IHttpClientFactory or HttpClient please? Q2 : And if IHttpClientFactory is injected, should I use factory.CreateClient() for each call? 回答1: Summary HttpClient can only be injected inside Typed clients for other usages, you need IHttpClientFactory In both scenarios, the

Injected HttpClient ignores IHttpClientFactory configuration

。_饼干妹妹 提交于 2020-08-02 05:32:43
问题 I've created a custom library which automatically sets up Polly policies for specific services which depend on HttpClient . This is done using the IServiceCollection extension methods and the typed client approach. A simplified example: public static IHttpClientBuilder SetUpFooServiceHttpClient(this IServiceCollection services) { return services .AddHttpClient<FooService>() .AddPolicyHandler(GetRetryPolicy()); } The example service: public class FooService { private readonly HttpClient

Blazor HttpClient 3.2.0 Get call throwing exception because response header content-type not compatible with GetFromJsonAsync

和自甴很熟 提交于 2020-07-22 06:08:36
问题 I am calling a webapi from a blazor 3.2.0 client: protected override async Task OnParametersSetAsync() { if (SelectedComplexId != Guid.Empty) { residents = await HttpClient.GetFromJsonAsync<List<ResidentDTO>>($"api/complex/residents/{SelectedComplexId.ToString()}"); } } An exception is being thrown because the GetFromJsonAsync expects a content-type header of application/json in response. This is the api action method: [HttpGet("/residents/{complexId}")] public async Task<IActionResult>

Adding handler to default http client in ASP.NET Core [duplicate]

走远了吗. 提交于 2020-07-21 06:27:18
问题 This question already has an answer here : adding a handler to all clients created via IHttpClientFactory? (1 answer) Closed 4 days ago . Is there a way to add handlers to the default HTTP client in ASP.NET Core? Something like this? .AddHttpClient() .AddHttpMessageHandler<Handler1>() .AddHttpMessageHandler<Handler2>(); 回答1: Documentation states that you can only add handlers or configure the inner most handler to named or typed clients. Reference Configure the HttpMessageHandler It may be

How do you use Basic Authentication with System.Net.Http.HttpClient?

会有一股神秘感。 提交于 2020-07-16 05:17:39
问题 I'm trying to implement a rest client in c# .net core that needs to first do Basic Authentication, then leverage a Bearer token in subsequent requests. When I try to do Basic Authentication in combination with client.PostAsync with a FormUrlEncodedContent object, I'm getting an exception: System.InvalidOperationException occurred in System.Net.Http.dll: 'Misused header name. Make sure request headers are used with HttpRequestMessage, response headers with HttpResponseMessage, and content

How do you use Basic Authentication with System.Net.Http.HttpClient?

↘锁芯ラ 提交于 2020-07-16 05:16:28
问题 I'm trying to implement a rest client in c# .net core that needs to first do Basic Authentication, then leverage a Bearer token in subsequent requests. When I try to do Basic Authentication in combination with client.PostAsync with a FormUrlEncodedContent object, I'm getting an exception: System.InvalidOperationException occurred in System.Net.Http.dll: 'Misused header name. Make sure request headers are used with HttpRequestMessage, response headers with HttpResponseMessage, and content