How to add a request header to dynamic C# API client proxy / How to customize dynamic C# API client proxy?
- ABP Framework version: 5.0
- User Interface: MVC
- Database provider: EF Core
Hi all, I have an ABP MVC server app and an ABP MVC client app. I'd like this MVC client application to be tenant-specific. That's why I am trying to add a request header to the client app's requests to recognize the tenant by the server app. I can do it with HttpClient but I'd like to get the benefit of "Dynamic C# API Client Proxy". Is there any way to add a request header to client proxies?
hi
If you are using the static proxy you can custom the base class(ClientProxyBase).
https://github.com/abpframework/abp/blob/dev/modules/identity/src/Volo.Abp.Identity.HttpApi.Client/ClientProxies/IdentityRoleClientProxy.cs https://github.com/abpframework/abp/blob/dev/modules/identity/src/Volo.Abp.Identity.HttpApi.Client/ClientProxies/IdentityRoleClientProxy.Generated.cs
Hi @maliming No, I am not using the static proxy, but I will have a look.
@ertugrulbaki27 hello,Have you solved the problem?
https://github.com/abpframework/abp/issues/11545#issuecomment-1034786064
u can use this method configure your httpclient.
u can use this method configure your httpclient.
thanks, but how to add the dynamic params to request header?
Hello @codeio , You need to replace DefaultProxyHttpClientFactory
Moreover I've done in this way.
[Dependency(ReplaceServices = true)]
[ExposeServices(typeof(IProxyHttpClientFactory))]
public class CustomProxyHttpClientFactory : IProxyHttpClientFactory, ITransientDependency
{
private readonly IHttpClientFactory _httpClientFactory;
private IRemoteServiceConfigurationProvider _remoteServiceConfigurationProvider;
private string _tenant;
public CustomProxyHttpClientFactory(IHttpClientFactory httpClientFactory, IRemoteServiceConfigurationProvider remoteServiceConfigurationProvider)
{
_httpClientFactory = httpClientFactory;
_remoteServiceConfigurationProvider = remoteServiceConfigurationProvider;
_tenant = _remoteServiceConfigurationProvider.GetConfigurationOrDefaultAsync("Default").Result["Tenant"];
}
public HttpClient Create()
{
var client = _httpClientFactory.CreateClient();
client.DefaultRequestHeaders.Add(TenantResolverConsts.DefaultTenantKey, _tenant);
return client;
}
public HttpClient Create(string name)
{
var client = _httpClientFactory.CreateClient(name);
client.DefaultRequestHeaders.Add(TenantResolverConsts.DefaultTenantKey, _tenant);
return client;
}
}
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.