httpclientfactory

Should HttpClient instances created by HttpClientFactory be disposed?

我的未来我决定 提交于 2019-11-27 17:47:31
问题 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.

Configure HttpClientFactory to use data from the current request context

僤鯓⒐⒋嵵緔 提交于 2019-11-26 23:09:17
问题 With the new HttpClientFactory in ASP.NET Core 2.1, it's quite easy to configure custom HTTP clients with things like base urls, default headers etc. However, I haven't found a way to centralize configuration that lets me inject headers from the current request context. For example, consider a service called with an Authorization header, which I wish to pass on to all the underlying services as well. It would be awesome to be able to configure this in the .AddHttpClient() call on services in

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

怎甘沉沦 提交于 2019-11-26 17:03:24
问题 .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