dotnet-httpclient

How to use ConfigurePrimaryHttpMessageHandler generic

∥☆過路亽.° 提交于 2020-05-29 06:13:31
问题 I want to add an HttClientHandler for a Typed HttpClient in order to include certificate authentication. All the examples I'm finding on the internet are like this: services.AddHttpClient<IMyService, MyService>() .ConfigurePrimaryHttpMessageHandler(() => { return new HttpClientHandler() { // Set here whatever you need to get configured }; }); But I don't want to include all the logic to obtain the certificate here, so I would like to use the generic version of

A suitable constructor for type 'ApplicationInventory.Services.ServerInventoryService' could not be located

丶灬走出姿态 提交于 2020-05-17 04:30:32
问题 I am trying to create a service that is accessible by both Controllers and regular functions for all API calls to a specific endpoint for my ASP.Net Core 2.2 application. Whenever I try and inject the HttpClient class an error occurs before the functions can be reached/ I have setup my startup.cs as shown in documentation and have reduced the constructor inputs to just HttpClient so I know that's where the root of the issue is. I have added the HttpClient to the configure services section fo

How do I test an https redirect in ASP.NET Core?

自闭症网瘾萝莉.ら 提交于 2020-05-15 15:12:30
问题 Recently, I came up with unit tests for checking some redirect rules of my ASP.NET Core 2.1 application: [Fact(DisplayName = "lowercase path")] public async Task LowercaseRedirect() { var result = await this.Client.GetAsync("/BLOG/"); Assert.EndsWith("/blog/", result.RequestMessage.RequestUri.PathAndQuery, StringComparison.InvariantCulture); } [Fact(DisplayName = "add missing slash")] public async Task SlashRedirect() { var result = await this.Client.GetAsync("/blog"); Assert.EndsWith("/blog/

How do I test an https redirect in ASP.NET Core?

拜拜、爱过 提交于 2020-05-15 15:10:14
问题 Recently, I came up with unit tests for checking some redirect rules of my ASP.NET Core 2.1 application: [Fact(DisplayName = "lowercase path")] public async Task LowercaseRedirect() { var result = await this.Client.GetAsync("/BLOG/"); Assert.EndsWith("/blog/", result.RequestMessage.RequestUri.PathAndQuery, StringComparison.InvariantCulture); } [Fact(DisplayName = "add missing slash")] public async Task SlashRedirect() { var result = await this.Client.GetAsync("/blog"); Assert.EndsWith("/blog/

Get response in JSON from API

霸气de小男生 提交于 2020-03-26 04:43:12
问题 I am using HttpClient to consume an external API from an ASP.NET Web API controller. I am not using authentication, just a token, so I have: using (var httpClient = new HttpClient()) { httpClient.DefaultRequestHeaders.Accept.Clear(); httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); HttpResponseMessage response = await httpClient.GetAsync(endpoint); } I am getting the response always in XML format but I am sending header with "application

Polly Circuit breaker not maintaining state with .net core HTTP Client

余生长醉 提交于 2020-03-26 03:52:11
问题 I have implemented the polly retry and Circuit breaker policy (wrapped). when the call fails and the circuit is open for the previous call the next call again goes to the retry and hit the circuit breaker again instead of just throwing the circuitbreakexception. I think somehow the HTTP client is getting recreated again even though am using the typed client. I am not able to figure the issue. Here is the code Startup public void ConfigureServices(IServiceCollection services) { services.AddMvc

Forwarding the entire received request “as is” using HttpClient

落爺英雄遲暮 提交于 2020-03-25 18:21:01
问题 I have a thin frontend API web service that does some preprocessing on the received data and then sends the data to my backend API web service using HttpClient. There are some complex cases when a request contains multipart data with JSON and files, and I don't want to parse it at all in the frontend. The backend will do the job. So, I would like to take the request "as is" - as raw as possible (not caring about its contents and whether it's multipart or not) and just forward it to the

Forwarding the entire received request “as is” using HttpClient

岁酱吖の 提交于 2020-03-25 18:20:44
问题 I have a thin frontend API web service that does some preprocessing on the received data and then sends the data to my backend API web service using HttpClient. There are some complex cases when a request contains multipart data with JSON and files, and I don't want to parse it at all in the frontend. The backend will do the job. So, I would like to take the request "as is" - as raw as possible (not caring about its contents and whether it's multipart or not) and just forward it to the

Use HTTP 2 with HttpClient in .Net

半城伤御伤魂 提交于 2020-03-13 06:24:31
问题 I'm trying to request data over HTTP 2.0. I'm using the HttpClient from .Net Core 2.2. I'm on Windows 10 but will run on Linux in production. The problem is that the version on the response seems to always be "1.1". What I am doing wrong? using (var client = new HttpClient()) { using (var request = new HttpRequestMessage(new HttpMethod("GET"), "https://duckduckgo.com/" )) { request.Version = new Version(2, 0); var response = await client.SendAsync(request); Console.WriteLine(response.Version)

Any way to differentiate Cancel and Timeout

不想你离开。 提交于 2020-03-13 04:29:10
问题 I have some code that is validating some data by making calls to a number of other services. I start all of the calls in parallel and then wait until at least one of them finishes. If any of the requests fail, I don't care about the result of the other calls. I make the calls with HttpClient and I have passed an HttpMessageHandler in that does a bunch of logging. Essentially: protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken