Skip to content

Instantly share code, notes, and snippets.

@ankitvijay
Last active January 8, 2021 02:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ankitvijay/9ed7ce02a3f54312d10991d989c5c421 to your computer and use it in GitHub Desktop.
Save ankitvijay/9ed7ce02a3f54312d10991d989c5c421 to your computer and use it in GitHub Desktop.
Captive Dependency 6 - Typed HttpClient example 2
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<SingletonService>();
services.AddSingleton<CatalogClientFactory>();
services.AddHttpClient<CatalogClient>();
}
public class CatalogClientFactory
{
private readonly IServiceProvider serviceProvider;
public CatalogClientFactory(IServiceProvider serviceProvider)
{
this.serviceProvider = serviceProvider;
}
public CatalogClient Create() => serviceProvider.GetService<CatalogClient>();
}
public class SingletonService
{
private readonly CatalogClientFactory catalogClientFactory;
public SingletonService(CatalogClientFactory catalogClientFactory)
{
this.catalogClientFactory = catalogClientFactory;
}
public async Task<List<string>> GetCatalogs()
{
return await catalogClientFactory.Create().GetCatalogs();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment