I'm trying to add IHttpClientFactory
to NInject in my C# ASP.NET Web Forms application so that I can use it in services. There is lots of documentation online about using IHttpClientFactory (and not creating lots of HttpClient
instances), however:
- Documentation/Examples are based on using the interface with .NET Core and I'm using ASP.NET (i.e. .NET Framework) e.g. https://learn.microsoft.com/en-us/dotnet/architecture/microservices/implement-resilient-applications/use-httpclientfactory-to-implement-resilient-http-requests, https://learn.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-3.0
- Documentation tends to be about wiring up
IHttpClientFactory
using Microsoft's own dependency injection system rather than NInject.
So, what have I tried? Well, I don't know where to start really as although IHttpClientFactory
is well defined in the System.Net.Http
namespace, there is no concrete implementation exposed in which I can bind it to. I want to write this:
public class ClassLibNinjectModule : NinjectModule
{
public override void Load()
{
Bind<System.Net.Http.IHttpClientFactory>().ToConstant<System.Net.Http.HttpClientFactory>();
}
}
However:
System.Net.Http.HttpClientFactory
is a static type and it doesn't implementSystem.Net.Http.IHttpClientFactory
(different methods:CreateClient()
vsCreate()
)- I can't seem to get how to bind a service to a static provider.
Can anyone advise where I'm going wrong? Thanks.