I have built a command-line app using .net core 3.0 using the Generic Host Builder. If I add a HttpClient to the services using AddHttpClient the the service requesting HttpClient fails to resolve with an error "Unable to resolve service for type 'System.Net.Http.HttpClient'"
If I register HttpClient myself then it resolves, so it appears that the AddHttpClient does not work unless it is a Http Server.
I find the whole AddHttpClient approach a bit more attractive as I can easily add policies etc. In fact I am trying to create a reusable component that I can share between applications(web and non-web) which builds a consistent HttpClient.
Building Host
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) =>
{
services.AddHttpClient(); // This fails to resolve HttpClient
//services.AddSingleton<HttpClient>(provider => new HttpClient()); //This resolves okay
services.AddSingleton<TestClass>();
});
Classes requiring HttpClient which fail to resolve
public class TestClass : ITestClass
{
public TestClass(HttpClient client)
{
}
}
public interface ITestClass
{
}
AddHttpClientjust adds the factory