kandi X-RAY | DownloadRedirect Summary
kandi X-RAY | DownloadRedirect Summary
DownloadRedirect
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of DownloadRedirect
DownloadRedirect Key Features
DownloadRedirect Examples and Code Snippets
Community Discussions
Trending Discussions on DownloadRedirect
QUESTION
I'm looking for suggestions on how to improve on my current design for testing a class (example below) that depends on HttpClient
with a custom HttpClientHandler
configuration. I normally use constructor injection to inject a HttpClient that is consistent across the application, however because this is in a class library I can't rely on the consumers of the library to set up the HttpClientHandler
correctly.
For testing I follow the standard approach of replacing HttpClientHandler
in the HttpClient
constructor. Because I can't rely on the consumer of the library to inject a valid HttpClient
I'm not putting this in a public constructor, instead I'm using a private constructor with an internal static method (CreateWithCustomHttpClient()
) to create it. The intent behind this is:
- Private constructor should not be called by a dependency injection library automatically. I'm aware that if I made it public/internal then some DI libraries that had a
HttpClient
already registered would call that constructor. - Internal static method can be called by a unit testing library using
InternalsVisibleToAttribute
This setup seems quite complex to me and I'm hoping someone might be able to suggest an improvement, I am however aware that this could be quite subjective so if there are any established patterns or design rules to follow in this case I would really appreciate hearing about them.
I've included the DownloadSomethingAsync()
method just to demonstrate why the non-standard configuration is required for HttpClientHandler
. The default is for redirect responses to automatically redirect internally without returning the response, I need the redirect response so that I can wrap it in a class that report progress on the download (the functionality of that is not relevant to this question).
ANSWER
Answered 2020-Apr-28 at 10:22In my opinion, I'd use IHttpClientFactory in Microsoft.Extensions.Http, and create a custom dependency injection extension for consumers of the class library to use:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install DownloadRedirect
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page