In my .NET Core console application, this line freezes:
services.GetRequiredService<ISomething>();
in the following code:
interface ISomething { }
class Cool : ISomething
{
public Cool(IHttpClientFactory factory) { }
}
class Program
{
static async Task Main(string[] args)
{
var host = CreateHostBuilder(args).Build();
await host.StartAsync();
// Freezes here and maxes out memory
ISomething internalApiConnector = host.Services.GetRequiredService<ISomething>();
await host.RunAsync();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureServices((_, services) =>
{
services.AddHttpClient<IHttpClientFactory>();
services.AddSingleton<ISomething, Cool>();
});
}
and ends up maxing out the memory with eventually crashes the app. What am I missing?
Note:
Coolconstructor does not get run, so doesn't appear to be something halting in there.
Solution (Thanks all):
serviceCollection.AddHttpClient<IHttpClientFactory>(); needed to be changed to
serviceCollection.AddHttpClient();
and
.BuildServiceProvider() was superflous.