August 10 2024
var builder = WebApplication.CreateBuilder(args); builder.Services.AddHttpClient<NewsletterService>(httpClient => { httpClient.BaseAddress = new Uri("https://api.newsletter.com"); }); var app = builder.Build(); app.Run();
public class NewsletterService(HttpClient client) { public async Task<NewsletterUser?> GeEmailByIdAsync(string id) { var endpoint = $"users/{id}"; return await client.GetFromJsonAsync<NewsletterUser>(endpoint); } public async Task<double> GetOpenRateAsync(string id) { var endpoint = $"issues/{id}/open-rate"; return await client.GetFromJsonAsync<double>(endpoint); } }
public class AuthenticationDelegatingHandler : DelegatingHandler { protected override Task<HttpResponseMessage> SendAsync( HttpRequestMessage request, CancellationToken cancellationToken) { //Don't hardcode those values request.Headers.Add("Authorization", "secretAccessToken"); request.Headers.Add("SomeOtherHeader", "someOtherValue"); return base.SendAsync(request, cancellationToken); } }
builder.Services.AddTransient<AuthenticationDelegatingHandler>(); builder.Services.AddHttpClient<NewsletterService>(httpClient => { httpClient.BaseAddress = new Uri("https://api.newsletter.com"); })
1. Design Patterns that Deliver
This isn’t just another design patterns book. Dive into real-world examples and practical solutions to real problems in real applications.Check out it here.
Go-to resource for understanding the core concepts of design patterns without the overwhelming complexity. In this concise and affordable ebook, I've distilled the essence of design patterns into an easy-to-digest format. It is a Beginner level. Check out it here.
Every Monday morning, I share 1 actionable tip on C#, .NET & Arcitecture topic, that you can use right away.
Join 18,000+ subscribers to improve your .NET Knowledge.
Subscribe to the TheCodeMan.net and be among the 18,000+ subscribers gaining practical tips and resources to enhance your .NET expertise.