Created
June 5, 2018 08:23
-
-
Save christiannagel/668130a6a1b3d5d1392d0d0797ed352d to your computer and use it in GitHub Desktop.
Using HttpClient via injecting IHttpClientFactory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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