我正在尝试将 Typed HttpClient 与 Autofac 一起使用。但我无法让它发挥作用。
public class MainViewModel
{
public MainViewModel(MoviesApiClient moviesApiClient, IHttpClientFactory httpClientFactory)
{
}
}
public class MoviesAPIClient
{
private HttpClient _client;
public MoviesAPIClient(HttpClient client)
{
_client = client;
_client.BaseAddress = new Uri("http://localhost:5001");
}
}
var builder = new ContainerBuilder();
builder.Register(ctx =>
{
var services = new ServiceCollection();
services.AddHttpClient<MoviesAPIClient>(configureClient =>{});
var provider = services.BuildServiceProvider();
return provider.GetRequiredService<IHttpClientFactory>();
});
builder.RegisterType<MainWindow>().AsSelf();
builder.RegisterType<MainViewModel>().AsSelf();
return builder.Build();
有人可以建议解决方案以使其发挥作用吗?
我通过返回正确的类型客户端“MoviesAPIClient”来修复它
builder.Register(ctx =>
{
var services = new ServiceCollection();
services.AddHttpClient<MoviesAPIClient>(configureClient =>{});
var provider = services.BuildServiceProvider();
return provider.GetRequiredService<MoviesAPIClient>();
});