Created
April 22, 2022 14:31
-
-
Save alexandrebl/750cfd839c3e6f6f5a2eb8b7c3252e97 to your computer and use it in GitHub Desktop.
Cliente Using Dependency Injection Named Http Client Factory
This file contains hidden or 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
| public class NamedClientModel : PageModel | |
| { | |
| private readonly IHttpClientFactory _httpClientFactory; | |
| public NamedClientModel(IHttpClientFactory httpClientFactory) => | |
| _httpClientFactory = httpClientFactory; | |
| public IEnumerable<GitHubBranch>? GitHubBranches { get; set; } | |
| public async Task OnGet() | |
| { | |
| var httpClient = _httpClientFactory.CreateClient("GitHub"); | |
| var httpResponseMessage = await httpClient.GetAsync( | |
| "repos/dotnet/AspNetCore.Docs/branches"); | |
| if (httpResponseMessage.IsSuccessStatusCode) | |
| { | |
| using var contentStream = | |
| await httpResponseMessage.Content.ReadAsStreamAsync(); | |
| GitHubBranches = await JsonSerializer.DeserializeAsync | |
| <IEnumerable<GitHubBranch>>(contentStream); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment