Browse Source

Merge pull request #14877 from abpframework/AbpDaprClientFactory

Add abp headers to Dapr's `HttpClient`.
pull/18095/head
maliming 3 years ago
committed by GitHub
parent
commit
86e7570cdf
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      framework/src/Volo.Abp.Dapr/Volo.Abp.Dapr.csproj
  2. 44
      framework/src/Volo.Abp.Dapr/Volo/Abp/Dapr/AbpDaprClientFactory.cs
  3. 3
      framework/src/Volo.Abp.Dapr/Volo/Abp/Dapr/AbpDaprModule.cs

1
framework/src/Volo.Abp.Dapr/Volo.Abp.Dapr.csproj

@ -12,6 +12,7 @@
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Volo.Abp.Json\Volo.Abp.Json.csproj" /> <ProjectReference Include="..\Volo.Abp.Json\Volo.Abp.Json.csproj" />
<ProjectReference Include="..\Volo.Abp.MultiTenancy.Abstractions\Volo.Abp.MultiTenancy.Abstractions.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

44
framework/src/Volo.Abp.Dapr/Volo/Abp/Dapr/AbpDaprClientFactory.cs

@ -1,10 +1,14 @@
using System; using System;
using System.Globalization;
using System.Net.Http; using System.Net.Http;
using System.Net.Http.Headers;
using System.Text.Json; using System.Text.Json;
using Dapr.Client; using Dapr.Client;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Volo.Abp.DependencyInjection; using Volo.Abp.DependencyInjection;
using Volo.Abp.Json.SystemTextJson; using Volo.Abp.Json.SystemTextJson;
using Volo.Abp.MultiTenancy;
using Volo.Abp.Tracing;
namespace Volo.Abp.Dapr; namespace Volo.Abp.Dapr;
@ -13,13 +17,21 @@ public class AbpDaprClientFactory : IAbpDaprClientFactory, ISingletonDependency
protected AbpDaprOptions DaprOptions { get; } protected AbpDaprOptions DaprOptions { get; }
protected JsonSerializerOptions JsonSerializerOptions { get; } protected JsonSerializerOptions JsonSerializerOptions { get; }
protected IDaprApiTokenProvider DaprApiTokenProvider { get; } protected IDaprApiTokenProvider DaprApiTokenProvider { get; }
protected ICurrentTenant CurrentTenant { get; }
protected ICorrelationIdProvider CorrelationIdProvider { get; }
protected IOptions<AbpCorrelationIdOptions> AbpCorrelationIdOptions { get; }
public AbpDaprClientFactory( public AbpDaprClientFactory(
IOptions<AbpDaprOptions> options, IOptions<AbpDaprOptions> options,
IOptions<AbpSystemTextJsonSerializerOptions> systemTextJsonSerializerOptions, IOptions<AbpSystemTextJsonSerializerOptions> systemTextJsonSerializerOptions,
IDaprApiTokenProvider daprApiTokenProvider) IDaprApiTokenProvider daprApiTokenProvider,
ICurrentTenant currentTenant, ICorrelationIdProvider correlationIdProvider,
IOptions<AbpCorrelationIdOptions> abpCorrelationIdOptions)
{ {
DaprApiTokenProvider = daprApiTokenProvider; DaprApiTokenProvider = daprApiTokenProvider;
CurrentTenant = currentTenant;
CorrelationIdProvider = correlationIdProvider;
AbpCorrelationIdOptions = abpCorrelationIdOptions;
DaprOptions = options.Value; DaprOptions = options.Value;
JsonSerializerOptions = CreateJsonSerializerOptions(systemTextJsonSerializerOptions.Value); JsonSerializerOptions = CreateJsonSerializerOptions(systemTextJsonSerializerOptions.Value);
} }
@ -61,11 +73,39 @@ public class AbpDaprClientFactory : IAbpDaprClientFactory, ISingletonDependency
daprEndpoint = DaprOptions.HttpEndpoint; daprEndpoint = DaprOptions.HttpEndpoint;
} }
return DaprClient.CreateInvokeHttpClient( var httpClient = DaprClient.CreateInvokeHttpClient(
appId, appId,
daprEndpoint, daprEndpoint,
daprApiToken ?? DaprApiTokenProvider.GetDaprApiToken() daprApiToken ?? DaprApiTokenProvider.GetDaprApiToken()
); );
AddHeaders(httpClient);
return httpClient;
}
protected virtual void AddHeaders(HttpClient httpClient)
{
//CorrelationId
httpClient.DefaultRequestHeaders.Add(AbpCorrelationIdOptions.Value.HttpHeaderName, CorrelationIdProvider.Get());
//TenantId
if (CurrentTenant.Id.HasValue)
{
//TODO: Use AbpAspNetCoreMultiTenancyOptions to get the key
httpClient.DefaultRequestHeaders.Add(TenantResolverConsts.DefaultTenantKey, CurrentTenant.Id.Value.ToString());
}
//Culture
//TODO: Is that the way we want? Couldn't send the culture (not ui culture)
var currentCulture = CultureInfo.CurrentUICulture.Name ?? CultureInfo.CurrentCulture.Name;
if (!currentCulture.IsNullOrEmpty())
{
httpClient.DefaultRequestHeaders.AcceptLanguage.Add(new StringWithQualityHeaderValue(currentCulture));
}
//X-Requested-With
httpClient.DefaultRequestHeaders.Add("X-Requested-With", "XMLHttpRequest");
} }
protected virtual JsonSerializerOptions CreateJsonSerializerOptions(AbpSystemTextJsonSerializerOptions systemTextJsonSerializerOptions) protected virtual JsonSerializerOptions CreateJsonSerializerOptions(AbpSystemTextJsonSerializerOptions systemTextJsonSerializerOptions)

3
framework/src/Volo.Abp.Dapr/Volo/Abp/Dapr/AbpDaprModule.cs

@ -4,10 +4,11 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.DependencyInjection.Extensions;
using Volo.Abp.Json; using Volo.Abp.Json;
using Volo.Abp.Modularity; using Volo.Abp.Modularity;
using Volo.Abp.MultiTenancy;
namespace Volo.Abp.Dapr; namespace Volo.Abp.Dapr;
[DependsOn(typeof(AbpJsonModule))] [DependsOn(typeof(AbpJsonModule), typeof(AbpMultiTenancyAbstractionsModule))]
public class AbpDaprModule : AbpModule public class AbpDaprModule : AbpModule
{ {
public override void ConfigureServices(ServiceConfigurationContext context) public override void ConfigureServices(ServiceConfigurationContext context)

Loading…
Cancel
Save