Is this a good idea and what is the advantage?
The whole idea behind the type client AddHttpClient<GitHubClient>
is that you want to hide the http based communication from the rest of the world. The GitHubClient
class can encapsulate all the following logic:
- Prepare request object
- Issue request
- Handle transient failure(s)
- Perform checks against the response
- Parse the response's body into a desired data structure
From the consumer of the GitHubClient
perspective it is all hidden. (S)he can only see strongly typed methods like
Task<List<Branch>> GetRemoteBranches()
Task<List<PullRequest>> GetPullRequests(PullRequestState stateFlag)
- ...
For further information I would recommend the following threads:
Will I get the not updating DNS issue with this? I’m guessing the IP address of my Azure BLOB storage will be changing regularly.
In case of .NET Core and .NET 5+ the HttpClient
instances are short living whereas the underlying HttpClientMessageHandler
are pooled and reused. You don't have to care about the DNS resolve issues any more
For further information I would recommend the following threads:
Do I still have to provide the URI for a specific file. So am I then just providing everything after the base? (This is a slight pain as I have these urls in the database as the full url - and they need to be that way.)
Yes that's correct. If you have set up the BaseAddress
then it will be always used as a prefix. So, in your requests you should only provide the relative url. Gladly you don't have to deal with the forward slashes, it will be resolved automatically. So, the bellow four code examples are issuing the same request against https://httpstat.us/200
url.
var httpClient = new HttpClient()
{ BaseAddress = new Uri("https://httpstat.us") }; //no trailing slash
var result = await httpClient.GetAsync("200"); //no leading slash
var httpClient = new HttpClient()
{ BaseAddress = new Uri("https://httpstat.us/") }; //trailing slash
var result = await httpClient.GetAsync("200"); //no leading slash
var httpClient = new HttpClient()
{ BaseAddress = new Uri("https://httpstat.us") }; //no trailing slash
var result = await httpClient.GetAsync("/200"); //leading slash
var httpClient = new HttpClient()
{ BaseAddress = new Uri("https://httpstat.us/") }; //trailing slash
var result = await httpClient.GetAsync("/200"); //leading slash