httpclientfactory

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

梦想的初衷 提交于 2021-01-29 10:00:44
问题 .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); 回答1: The HttpClientFactory is derived from

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

Can I use HttpClientFactory in a .NET.core app which is not ASP.NET Core?

为君一笑 提交于 2020-07-08 04:00:52
问题 I have read the popular blog post https://www.stevejgordon.co.uk/introduction-to-httpclientfactory-aspnetcore on using HttpClientFactory To quote from it A new HttpClientFactory feature is coming in ASP.NET Core 2.1 which helps to solve some common problems that developers may run into when using HttpClient instances to make external web requests from their applications. All the examples show wiring it up in startup class of asp.net application e.g. public void ConfigureServices

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

How to Refresh a token using IHttpClientFactory

谁都会走 提交于 2020-04-29 15:48:43
问题 I am using IHttpClientFactory for sending requests and receiving HTTP responses from two external APIs using Net Core 2.2. I am looking for a good strategy to get a new access token using a refresh token that has been stored in the appsettings.json. The new access token needs to be requested when the current request returns 403 or 401 errors, When the new access and refresh token have been obtained, the appsettings.json needs to be updated with the new values in order to be used in subsequent

How to Refresh a token using IHttpClientFactory

给你一囗甜甜゛ 提交于 2020-04-29 15:44:47
问题 I am using IHttpClientFactory for sending requests and receiving HTTP responses from two external APIs using Net Core 2.2. I am looking for a good strategy to get a new access token using a refresh token that has been stored in the appsettings.json. The new access token needs to be requested when the current request returns 403 or 401 errors, When the new access and refresh token have been obtained, the appsettings.json needs to be updated with the new values in order to be used in subsequent

Attaching httpHandler to httpclientFactory webapi aspnetcore 2.1

拜拜、爱过 提交于 2020-02-02 12:14: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

Attaching httpHandler to httpclientFactory webapi aspnetcore 2.1

僤鯓⒐⒋嵵緔 提交于 2020-02-02 12:14:08
问题 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

Autofac fails to resolve enumerable of typed HttpClients

大兔子大兔子 提交于 2020-01-25 00:31:24
问题 I have a number of services which require usage of typed HttpClient from HttpClientFactory. Though I can resolve one service I can't resolve IEnumerable of this services. interface IMyHttpClient { } class MyHttpClient: IMyHttpClient { public MyHttpClient(HttpClient client) { } } class Program { static void Main(string[] args) { var services = new ServiceCollection(); services.AddHttpClient() .AddHttpClient<IMyHttpClient, MyHttpClient>(); var builder = new ContainerBuilder(); // Exception goes