.NET Core - Addhttpclient returning null

Asked

Viewed 8 times

0

In an ASP.NET Webapi project I am trying to register a typed http client in the DI container. But when trying to configure using the service Provider ends up returning null in the service.

Ambiente de runtime:
 OS Name:     Windows
 OS Version:  10.0.19043
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\5.0.203\

Host (useful for support):
  Version: 5.0.6
  Commit:  478b2f8c0e

.NET SDKs installed:
  3.1.407 [C:\Program Files\dotnet\sdk]
  5.0.203 [C:\Program Files\dotnet\sdk]

Here’s an example of how I tried to set up

public interface IClient {}
public class Client : IClient {}

//No startup
services.Configure<Config>(config => new Config());
services.AddHttpClient<IClient, Client>((provider, http) =>
{
  var config = provider.GetRequiredService<IOptions<Config>>().Value;
  http.BaseAddress = new Uri(config.Url.Value);
});

But when inspecting the Servicecollection shows the following inserir a descrição da imagem aqui

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.