I'm registering my HttpClient like this:
builder.Services.AddHttpClient<IMyClass, MyClass>(i =>
{
i.BaseAddress = new Uri("https://google.com"); //breakpoint here never stops
});
And MyClass
's constructor is implemented like
public MyClass(HttpClient client)
{
// client.BaseAddress is null here
}
What am I doing wrong? My expectation here is the client
on the concrete class comes pre-configured...
I have already tried registering like:
AddHttpClient("global", i => { i.BaseAddress = new Uri("https://google.com"); }
AddHttpClient<MyClass>(i => { i.BaseAddress = new Uri("https://google.com"); }
AddHttpClient<IMyClass>(i => { i.BaseAddress = new Uri("https://google.com"); }
I've encountered the same issue while writing unit tests, and the cause is internal service registration introduced with .NET 8. See here: DefaultHttpClientBuilder - dotnet/runtime
They're trying to resolve that tracker class, using LINQ .Single()
method, because they aren't expecting that registered class is not present.
I've currently disabled that specific unit test, but I'm sorry that does not solve your issue at all because they broke it (.NET 7 and earlier versions do not have this issue.)