httpclientfactory

How to use HttpClientHandler with HttpClientFactory in .NET Core

半城伤御伤魂 提交于 2019-12-30 05:44:08
问题 I want to use the HttpClientFactory that is available in .NET Core 2.1 but I also want to use the HttpClientHandler to utilize the AutomaticDecompression property when creating HttpClients . I am struggling because the .AddHttpMessageHandler<> takes a DelegatingHandler not a HttpClientHandler . Does anyone know how to get this to work? Thanks, Jim 回答1: Actually I'm not using automatic decompression but the way to achieve this is to properly register http client services.AddHttpClient

How to Read Parameters sent to an Action Method (WebAPI) within a DelegatingHandler

家住魔仙堡 提交于 2019-12-11 05:15:38
问题 I am using IHttpClientFactory for sending requests and receiving HTTP responses from an external APIs using Net Core 2.2. I have implemented a DelegatingHandler to "intercept" my http request and add the Authorization header (Token). if token is not valid, It gets a new token and retry one more time. Likewise, when I get a new token for the first time, I cache the token in-memory for further references. For caching the token I have created a dictionary that requires an accountID and the token

Certificate error when configuring HttpClientFactory

余生颓废 提交于 2019-12-07 08:21:48
问题 I need to add certificate in HttpClientFactory . Old implementation with HttpClient look this: var cookieContainer = new CookieContainer(); var handler = new HttpClientHandler { CookieContainer = cookieContainer }; var basePath = Directory.GetCurrentDirectory(); var certificatePath = Path.Combine(basePath, certPath); var fileExists = File.Exists(certificatePath); if (!fileExists) throw new ArgumentException(certificatePath); var certificate = new X509Certificate2(certificatePath, certPwd);

Attaching httpHandler to httpclientFactory webapi aspnetcore 2.1

一笑奈何 提交于 2019-12-06 12:06:46
I am trying to attach an handler to httpclientfactory using "ConfigurePrimaryHttpMessageHandler" but when I look inside the HttpClient to see if the handler is there i cannot find it Am I attaching the handler correctly? Any Suggesions services.AddHttpClient<IGitHubClient,GitHubClient>(client => { client.BaseAddress = new Uri(myUri); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); }) .ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler { AllowAutoRedirect = false, AutomaticDecompression = DecompressionMethods.Deflate |

Certificate error when configuring HttpClientFactory

不想你离开。 提交于 2019-12-05 14:52:38
I need to add certificate in HttpClientFactory . Old implementation with HttpClient look this: var cookieContainer = new CookieContainer(); var handler = new HttpClientHandler { CookieContainer = cookieContainer }; var basePath = Directory.GetCurrentDirectory(); var certificatePath = Path.Combine(basePath, certPath); var fileExists = File.Exists(certificatePath); if (!fileExists) throw new ArgumentException(certificatePath); var certificate = new X509Certificate2(certificatePath, certPwd); handler.ClientCertificates.Add(certificate); using (var client = new HttpClient(handler)) { client

Is it safe to use HttpClientFactory?

蹲街弑〆低调 提交于 2019-12-04 03:19:07
问题 In my asp.net core MVC application, I'm using HttpClientFactory to create HttpClient object for requests to API server. Follows Microsoft document, HttpClient object is created new for each time I call HttpClientFactory.CreateClient() , so it will be safe for setting values to HttpClient.DefaultRequestHeaders . About HttpMessageHandler objects, because they are pooled and can be re-used later. So, if they hold cookies information (For example: setting cookies to HttpClientHandler object), we

How to use HttpClientHandler with HttpClientFactory in .NET Core

房东的猫 提交于 2019-12-04 01:31:26
I want to use the HttpClientFactory that is available in .NET Core 2.1 but I also want to use the HttpClientHandler to utilize the AutomaticDecompression property when creating HttpClients . I am struggling because the .AddHttpMessageHandler<> takes a DelegatingHandler not a HttpClientHandler . Does anyone know how to get this to work? Thanks, Jim Actually I'm not using automatic decompression but the way to achieve this is to properly register http client services.AddHttpClient<MyCustomHttpClient>() .ConfigureHttpMessageHandlerBuilder((c) => new HttpClientHandler() { AutomaticDecompression =

Is it safe to use HttpClientFactory?

蓝咒 提交于 2019-12-01 17:06:12
In my asp.net core MVC application, I'm using HttpClientFactory to create HttpClient object for requests to API server. Follows Microsoft document, HttpClient object is created new for each time I call HttpClientFactory.CreateClient() , so it will be safe for setting values to HttpClient.DefaultRequestHeaders . About HttpMessageHandler objects, because they are pooled and can be re-used later. So, if they hold cookies information (For example: setting cookies to HttpClientHandler object), we will violate thread-safe. Is my assumption is correct? How could we deal with this problem? Is it OK if

Should HttpClient instances created by HttpClientFactory be disposed?

喜夏-厌秋 提交于 2019-11-29 06:59:44
So, I've registered a named client with the services collection in my Startup.cs: services.AddHttpClient(someServiceName, client => client.BaseAddress = baseAddress); and now can inject an IHttpClientFactory from my service provider. Using this IHttpClientFactory , I conjure up a client instance: var client = httpClientFactory.CreateClient(someServiceName) Once upon a time, it was necessary to be very careful about the disposing of HttpClient instances, as it was rarely the right thing to do . However, now we have HttpClientFactory , does this matter any more? Should/Can this client be

How to mock the new HttpClientFactory in .NET Core 2.1 using Moq

一笑奈何 提交于 2019-11-28 01:18:31
.NET Core 2.1 comes with this new factory called HTTPClientFactory , but I can't figure out how to mock it to unit test some methods that include REST service calls. The factory is being injected using .NET Core IoC container, and what the method does is create a new client from the factory: var client = _httpClientFactory.CreateClient(); And then using the client to get data from a REST service: var result = await client.GetStringAsync(url); The HttpClientFactory is derived from IHttpClientFactory Interface So it is just a matter of creating a mock of the interface var mockFactory = new Mock