- ABP Framework version: v4.4.3
- UI type: MVC
- DB provider: EF Core
- Tiered (MVC) or Identity Server Separated (Angular): No
- Exception message and stack trace:
- Steps to reproduce the issue:"
I'm storing tenant level configuration using the SettingsManager on each tenant. I'd like to register an httpclient in configureservices for each tenant using this configuration data for each tenant.
Whats the correct way to do this? I would need to
- Get the list of tenants
- Change to each tenant
- Retrieve the configuration with the setting manager?
- Register the named httpclient with the configuration values (baseurl, etc)
Could you provide an example or documentation of how to consume these services (settingmanager?, tenantmanager?) in configure service method of the application module?
I can then use the correct httpclient when a call is needed to the external remote service of that tenant.
What happens when the tenant updates their configuration, I'd need to deregister and register a new httpclient for them?
11 Answer(s)
- 
    0Hi, Have you tried to configure your HttpContext in ConfigureServices: context.Services.AddTransient<HttpClient>(sp => { // Get services you need. var currentTenant = sp.GetService<ICurrentTenant>(); var settingManager = sp.GetService<ISettingManager>(); // Create instance of HttpClient var httpClient = new HttpClient(); // Make your modifications here httpClient.DefaultRequestHeaders.Add("Tenant", currentTenant.Name); // Return it to DI Container, It'll inject where you use. return httpClient; });
- 
    0Also you can use AsyncHelper to execute async actions in ConfigureServices: var baseAddress = AsyncHelper.RunSync(() => settingManager.GetOrNullForCurrentTenantAsync("BaseAddress")); httpClient.BaseAddress = new Uri(baseAddress);
- 
    0Hi Enisn, Thanks for the feedback on this. I will try this, but I thought it was bad practice to spool up new httpclients everytime it was needed and instead we should use httpclientfactory? Can I share what I have done so far for this point and get your feedback? P.S before opening the question I tried for some hours and couldnt get any service back in configure service, always receive the exception System.ArgumentNullException: 'Value cannot be null. (Parameter 'provider')' 
- 
    0Hi Enisn, Thanks for the feedback on this. I will try this, but I thought it was bad practice to spool up new httpclients everytime it was needed and instead we should use httpclientfactory? Can I share what I have done so far for this point and get your feedback? P.S before opening the question I tried for some hours and couldnt get any service back in configure service, always receive the exception System.ArgumentNullException: 'Value cannot be null. (Parameter 'provider')' Can you share more code? I can't understand from this line why your ServiceProvider is null 
- 
    0
- 
    0You can't call a service out of a Configure method, because DI Container hasn't be built yet. In the .AddTransient method, I'm sure it works. When you inject a HttpClient, that method will be executed and create a HttpClient to inject to your required class. If you want to debug, put a breakpoint inside .AddTransient() method, and create a request to an method of appservice which injects HttpClient 
- 
    0Hi Enisn, Thanks for the info, yes I know how to add an http client, if you check my original question here its how to loop through the list of tenants so i can add http clients for each tenant, could you please provide an example of how to loop the tenants in my configure services so i can add required httpclients? You can see currently I'm doing this, I'm missing the tenant list and specific settings part. 
- 
    0bump 
- 
    0hi rcalv002 Maybe you can consider to use AbpDynamicOptionsManager, CallSetAsyncmethod before using HTTP client.https://github.com/abpframework/abp/pull/6333#issue-749741707 
- 
    0Hi Maliming Can I list the tenants in my DB in my configureservices on application module? If yes, can you show a sample please? 
- 
    0Can I list the tenants in my DB in my configureservices on application module? The built-in service does not support this. 



 
                                