abp icon indicating copy to clipboard operation
abp copied to clipboard

How to add a request header to dynamic C# API client proxy / How to customize dynamic C# API client proxy?

Open ertugrulbakidal opened this issue 4 years ago • 7 comments

  • 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?

ertugrulbakidal avatar Dec 27 '21 22:12 ertugrulbakidal

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

maliming avatar Dec 28 '21 02:12 maliming

Hi @maliming No, I am not using the static proxy, but I will have a look.

ertugrulbakidal avatar Dec 28 '21 10:12 ertugrulbakidal

@ertugrulbaki27 hello,Have you solved the problem?

yinghongzhen avatar Feb 17 '22 07:02 yinghongzhen

https://github.com/abpframework/abp/issues/11545#issuecomment-1034786064

u can use this method configure your httpclient.

mehmetuken avatar Feb 18 '22 10:02 mehmetuken

#11545 (comment)

u can use this method configure your httpclient.

thanks, but how to add the dynamic params to request header?

yinghongzhen avatar Mar 06 '22 08:03 yinghongzhen

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;
    }
}

ertugrulbakidal avatar Mar 06 '22 11:03 ertugrulbakidal

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.

stale[bot] avatar Jun 12 '22 10:06 stale[bot]