Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save alexandrebl/750cfd839c3e6f6f5a2eb8b7c3252e97 to your computer and use it in GitHub Desktop.

Select an option

Save alexandrebl/750cfd839c3e6f6f5a2eb8b7c3252e97 to your computer and use it in GitHub Desktop.
Cliente Using Dependency Injection Named Http Client Factory
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