Will the following IOC registration work with static extension calls to Flurl? The switch to injecting FlurlClient
is project wide, so I'm wondering if there is a way to register the MyFlurlClientFactory
by IOC
services.AddSingleton<IHttpClientFactory, MyFlurlClientFactory>();
Sample static extension call to Flurl.
string text = await "http://example.com/readme.txt".GetStringAsync();
CodePudding user response:
This will not work, because the static extension method knows nothing about instance-based ServiceCollection.
You can achieve that without DI, just by modifying the GlobalSettings at application startup before any flurl requests:
FlurlHttp.GlobalSettings.HttpClientFactory = new MyFlurlClientFactory();
And then call:
string text = await "https://flurl.dev/docs/client-lifetime/#using-flurl-with-an-ioc-container".GetStringAsync();
See the fully working example on the dotnetfiddle.
See more about configuration.
But the clearest DI-friendly solution I think just use the recommended documentation way - inject the IFlurlClient.