-
-
Notifications
You must be signed in to change notification settings - Fork 240
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for AddHttpClient #180
Comments
Hi @jasontaylordev! 👋🏻 It could technically be accomplished using an existing extensibility point, using Microsoft.Extensions.DependencyInjection;
namespace Scrutor;
public static class HttpClientExtensions
{
public static IServiceTypeSelector WithHttpClient(this IServiceTypeSelector selector)
{
return selector.UsingRegistrationStrategy(HttpClientRegistrationStrategy.Instance);
}
private class HttpClientRegistrationStrategy : RegistrationStrategy
{
public static readonly RegistrationStrategy Instance = new HttpClientRegistrationStrategy();
public override void Apply(IServiceCollection services, ServiceDescriptor descriptor)
{
// TODO: Implement call to AddHttpClient, based on descriptor, using reflection...
// services.AddHttpClient<TClient, TImplementation>();
}
}
} ...but there's a couple of reasons why shipping something like this out-of-the-box is problematic;
Also, quite often, you'd want to configure the |
If you could convince Microsoft to add this, it would be pretty easy to write, but looking at the code, the generic seems to go pretty deep, so it might be a hard ask 😅 |
No matter the implementation, this would be a nice thing to have as we too have several registrations done via AddHttpClient and all the rest is managed by Scrutor. |
Hey, I stumpled across this and thought I'd share an example of how to implement a simplistic version for AddHttpClient without any additional parameters being passed through:
This is mostly ChatGPT generated code so take it with a grain of salt. But it seems to be working so far and a good starting point for others looking into adding HttpClients via Scrutor :-) |
Just wondering if there is currently any way to support registration of Typed clients?
For example:
Use IHttpClientFactory to implement resilient HTTP requests | Microsoft Docs
The implementation might look something like this:
Thanks! 🙂
The text was updated successfully, but these errors were encountered: