diff --git a/framework/src/Volo.Abp.Dapr/Volo.Abp.Dapr.csproj b/framework/src/Volo.Abp.Dapr/Volo.Abp.Dapr.csproj index 34a505110c..3d1a6e9532 100644 --- a/framework/src/Volo.Abp.Dapr/Volo.Abp.Dapr.csproj +++ b/framework/src/Volo.Abp.Dapr/Volo.Abp.Dapr.csproj @@ -11,12 +11,14 @@ + - + + diff --git a/framework/src/Volo.Abp.Dapr/Volo/Abp/Dapr/AbpDaprClientFactory.cs b/framework/src/Volo.Abp.Dapr/Volo/Abp/Dapr/AbpDaprClientFactory.cs index 92a2961cd4..d78b65b5af 100644 --- a/framework/src/Volo.Abp.Dapr/Volo/Abp/Dapr/AbpDaprClientFactory.cs +++ b/framework/src/Volo.Abp.Dapr/Volo/Abp/Dapr/AbpDaprClientFactory.cs @@ -3,9 +3,13 @@ using System.Globalization; using System.Net.Http; using System.Net.Http.Headers; using System.Text.Json; +using System.Threading.Tasks; using Dapr.Client; +using IdentityModel.Client; using Microsoft.Extensions.Options; using Volo.Abp.DependencyInjection; +using Volo.Abp.Http.Client; +using Volo.Abp.Http.Client.Authentication; using Volo.Abp.Json.SystemTextJson; using Volo.Abp.MultiTenancy; using Volo.Abp.Tracing; @@ -20,23 +24,26 @@ public class AbpDaprClientFactory : IAbpDaprClientFactory, ISingletonDependency protected ICurrentTenant CurrentTenant { get; } protected ICorrelationIdProvider CorrelationIdProvider { get; } protected IOptions AbpCorrelationIdOptions { get; } + protected IRemoteServiceHttpClientAuthenticator RemoteServiceHttpClientAuthenticator { get; } public AbpDaprClientFactory( IOptions options, IOptions systemTextJsonSerializerOptions, IDaprApiTokenProvider daprApiTokenProvider, ICurrentTenant currentTenant, ICorrelationIdProvider correlationIdProvider, - IOptions abpCorrelationIdOptions) + IOptions abpCorrelationIdOptions, + IRemoteServiceHttpClientAuthenticator remoteServiceHttpClientAuthenticator) { DaprApiTokenProvider = daprApiTokenProvider; CurrentTenant = currentTenant; CorrelationIdProvider = correlationIdProvider; AbpCorrelationIdOptions = abpCorrelationIdOptions; + RemoteServiceHttpClientAuthenticator = remoteServiceHttpClientAuthenticator; DaprOptions = options.Value; JsonSerializerOptions = CreateJsonSerializerOptions(systemTextJsonSerializerOptions.Value); } - public virtual DaprClient Create(Action? builderAction = null) + public virtual Task CreateAsync(Action? builderAction = null) { var builder = new DaprClientBuilder() .UseJsonSerializationOptions(JsonSerializerOptions); @@ -59,10 +66,10 @@ public class AbpDaprClientFactory : IAbpDaprClientFactory, ISingletonDependency builderAction?.Invoke(builder); - return builder.Build(); + return Task.FromResult(builder.Build()); } - public virtual HttpClient CreateHttpClient( + public virtual async Task CreateHttpClientAsync( string? appId = null, string? daprEndpoint = null, string? daprApiToken = null) @@ -81,6 +88,22 @@ public class AbpDaprClientFactory : IAbpDaprClientFactory, ISingletonDependency AddHeaders(httpClient); + var request = new HttpRequestMessage(); + await RemoteServiceHttpClientAuthenticator.Authenticate( + new RemoteServiceHttpClientAuthenticateContext( + httpClient, + request, + new RemoteServiceConfiguration("/"), + string.Empty + ) + ); + + var bearerToken = request.Headers.Authorization?.Parameter; + if (!bearerToken.IsNullOrWhiteSpace()) + { + httpClient.SetBearerToken(bearerToken); + } + return httpClient; } diff --git a/framework/src/Volo.Abp.Dapr/Volo/Abp/Dapr/AbpDaprModule.cs b/framework/src/Volo.Abp.Dapr/Volo/Abp/Dapr/AbpDaprModule.cs index 65605bc342..06c8c0c4bc 100644 --- a/framework/src/Volo.Abp.Dapr/Volo/Abp/Dapr/AbpDaprModule.cs +++ b/framework/src/Volo.Abp.Dapr/Volo/Abp/Dapr/AbpDaprModule.cs @@ -2,13 +2,19 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; +using Volo.Abp.Http.Client; using Volo.Abp.Json; using Volo.Abp.Modularity; using Volo.Abp.MultiTenancy; +using Volo.Abp.RemoteServices; namespace Volo.Abp.Dapr; -[DependsOn(typeof(AbpJsonModule), typeof(AbpMultiTenancyAbstractionsModule))] +[DependsOn( + typeof(AbpJsonModule), + typeof(AbpMultiTenancyAbstractionsModule), + typeof(AbpHttpClientModule) +)] public class AbpDaprModule : AbpModule { public override void ConfigureServices(ServiceConfigurationContext context) @@ -16,12 +22,6 @@ public class AbpDaprModule : AbpModule var configuration = context.Services.GetConfiguration(); ConfigureDaprOptions(configuration); - - context.Services.TryAddSingleton( - serviceProvider => serviceProvider - .GetRequiredService() - .Create() - ); } private void ConfigureDaprOptions(IConfiguration configuration) diff --git a/framework/src/Volo.Abp.Dapr/Volo/Abp/Dapr/IAbpDaprClientFactory.cs b/framework/src/Volo.Abp.Dapr/Volo/Abp/Dapr/IAbpDaprClientFactory.cs index bbf074af6c..0fdb05ea6d 100644 --- a/framework/src/Volo.Abp.Dapr/Volo/Abp/Dapr/IAbpDaprClientFactory.cs +++ b/framework/src/Volo.Abp.Dapr/Volo/Abp/Dapr/IAbpDaprClientFactory.cs @@ -1,14 +1,15 @@ using System; using System.Net.Http; +using System.Threading.Tasks; using Dapr.Client; namespace Volo.Abp.Dapr; public interface IAbpDaprClientFactory { - DaprClient Create(Action? builderAction = null); + Task CreateAsync(Action? builderAction = null); - HttpClient CreateHttpClient( + Task CreateHttpClientAsync( string? appId = null, string? daprEndpoint = null, string? daprApiToken = null diff --git a/framework/src/Volo.Abp.DistributedLocking.Dapr/Volo/Abp/DistributedLocking/Dapr/DaprAbpDistributedLock.cs b/framework/src/Volo.Abp.DistributedLocking.Dapr/Volo/Abp/DistributedLocking/Dapr/DaprAbpDistributedLock.cs index 100fece989..335fd3d930 100644 --- a/framework/src/Volo.Abp.DistributedLocking.Dapr/Volo/Abp/DistributedLocking/Dapr/DaprAbpDistributedLock.cs +++ b/framework/src/Volo.Abp.DistributedLocking.Dapr/Volo/Abp/DistributedLocking/Dapr/DaprAbpDistributedLock.cs @@ -31,7 +31,7 @@ public class DaprAbpDistributedLock : IAbpDistributedLock, ITransientDependency { name = DistributedLockKeyNormalizer.NormalizeKey(name); - var daprClient = DaprClientFactory.Create(); + var daprClient = await DaprClientFactory.CreateAsync(); var lockResponse = await daprClient.Lock( DistributedLockDaprOptions.StoreName, name, diff --git a/framework/src/Volo.Abp.EventBus.Dapr/Volo/Abp/EventBus/Dapr/DaprDistributedEventBus.cs b/framework/src/Volo.Abp.EventBus.Dapr/Volo/Abp/EventBus/Dapr/DaprDistributedEventBus.cs index f4c3e6242f..ce6dee2cfc 100644 --- a/framework/src/Volo.Abp.EventBus.Dapr/Volo/Abp/EventBus/Dapr/DaprDistributedEventBus.cs +++ b/framework/src/Volo.Abp.EventBus.Dapr/Volo/Abp/EventBus/Dapr/DaprDistributedEventBus.cs @@ -255,7 +255,7 @@ public class DaprDistributedEventBus : DistributedEventBusBase, ISingletonDepend protected virtual async Task PublishToDaprAsync(string eventName, object eventData, Guid? messageId = null, string? correlationId = null) { - var client = DaprClientFactory.Create(); + var client = await DaprClientFactory.CreateAsync(); var data = new AbpDaprEventData(DaprEventBusOptions.PubSubName, eventName, (messageId ?? GuidGenerator.Create()).ToString("N"), Serializer.SerializeToString(eventData), correlationId); await client.PublishEventAsync(pubsubName: DaprEventBusOptions.PubSubName, topicName: eventName, data: data); }