Skip to content

Instantly share code, notes, and snippets.

@christiannagel
Created June 5, 2018 08:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christiannagel/668130a6a1b3d5d1392d0d0797ed352d to your computer and use it in GitHub Desktop.
Save christiannagel/668130a6a1b3d5d1392d0d0797ed352d to your computer and use it in GitHub Desktop.
Using HttpClient via injecting IHttpClientFactory
class ControllerUsingClientFactory
{
private readonly IHttpClientFactory _httpClientFactory;
public ControllerUsingClientFactory(IHttpClientFactory clientFactory)
{
_httpClientFactory = clientFactory ?? throw new ArgumentNullException(nameof(clientFactory));
}
public async Task CallServerAsync()
{
HttpClient client = _httpClientFactory.CreateClient("cni");
var response = await client.GetAsync("/downloads/Racers.xml");
response.EnsureSuccessStatusCode();
string content = await response.Content.ReadAsStringAsync();
Console.WriteLine(content);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment