Last active
January 8, 2021 02:01
-
-
Save ankitvijay/9ed7ce02a3f54312d10991d989c5c421 to your computer and use it in GitHub Desktop.
Captive Dependency 6 - Typed HttpClient example 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void ConfigureServices(IServiceCollection services) | |
{ | |
services.AddSingleton<SingletonService>(); | |
services.AddSingleton<CatalogClientFactory>(); | |
services.AddHttpClient<CatalogClient>(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CatalogClientFactory | |
{ | |
private readonly IServiceProvider serviceProvider; | |
public CatalogClientFactory(IServiceProvider serviceProvider) | |
{ | |
this.serviceProvider = serviceProvider; | |
} | |
public CatalogClient Create() => serviceProvider.GetService<CatalogClient>(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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