From 2d05d9b68de1918064e9936b7c5349ceb1830b92 Mon Sep 17 00:00:00 2001 From: maliming Date: Mon, 13 Nov 2023 10:39:47 +0800 Subject: [PATCH 1/2] Make `IAbpDaprClientFactory` async. Resolve #18090 --- .../src/Volo.Abp.Dapr/Volo.Abp.Dapr.csproj | 4 ++- .../Volo/Abp/Dapr/AbpDaprClientFactory.cs | 31 ++++++++++++++++--- .../Volo/Abp/Dapr/AbpDaprModule.cs | 14 ++++----- .../Volo/Abp/Dapr/IAbpDaprClientFactory.cs | 5 +-- .../Dapr/DaprAbpDistributedLock.cs | 2 +- .../EventBus/Dapr/DaprDistributedEventBus.cs | 2 +- 6 files changed, 42 insertions(+), 16 deletions(-) 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); } From 7668ba2aa3b4c3c0e9a8f1b28d06c04e09f30ded Mon Sep 17 00:00:00 2001 From: maliming Date: Mon, 13 Nov 2023 14:39:19 +0800 Subject: [PATCH 2/2] Update `Dapr` document. --- docs/en/Dapr/Index.md | 30 +++--------------------------- docs/zh-Hans/Dapr/Index.md | 30 +++--------------------------- 2 files changed, 6 insertions(+), 54 deletions(-) diff --git a/docs/en/Dapr/Index.md b/docs/en/Dapr/Index.md index 060b31aac3..464b56ea30 100644 --- a/docs/en/Dapr/Index.md +++ b/docs/en/Dapr/Index.md @@ -60,29 +60,6 @@ Alternatively, you can configure the options in the `Dapr` section of your `apps } ```` -### Injecting DaprClient - -ABP registers the `DaprClient` class to the [dependency injection](../Dependency-Injection.md) system. So, you can inject and use it whenever you need: - -````csharp -public class MyService : ITransientDependency -{ - private readonly DaprClient _daprClient; - - public MyService(DaprClient daprClient) - { - _daprClient = daprClient; - } - - public async Task DoItAsync() - { - // TODO: Use the injected _daprClient object - } -} -```` - -Injecting `DaprClient` is the recommended way of using it in your application code. When you inject it, the `IAbpDaprClientFactory` service is used to create it, which is explained in the next section. - ### IAbpDaprClientFactory `IAbpDaprClientFactory` can be used to create `DaprClient` or `HttpClient` objects to perform operations on Dapr. It uses `AbpDaprOptions`, so you can configure the settings in a central place. @@ -113,15 +90,14 @@ public class MyService : ITransientDependency }); // Create an HttpClient object - HttpClient httpClient = await _daprClientFactory - .CreateHttpClientAsync("target-app-id"); + HttpClient httpClient = await _daprClientFactory.CreateHttpClientAsync("target-app-id"); } } ```` `CreateHttpClientAsync` method also gets optional `daprEndpoint` and `daprApiToken` parameters. -> ABP uses `IAbpDaprClientFactory` when it needs to create a Dapr client. You can also use Dapr API to create client objects in your application. Using `IAbpDaprClientFactory` is recommended, but not required. +> You can use Dapr API to create client objects in your application. Using `IAbpDaprClientFactory` is recommended, but not required. ## C# API Client Proxies Integration @@ -412,7 +388,7 @@ Or you can set it in your `appsettings.json` file: } ```` -Once you set it, it is used when you inject `DaprClient` or use `IAbpDaprClientFactory`. If you need that value in your application, you can inject `IDaprApiTokenProvider` and use its `GetDaprApiToken()` method. +Once you set it, it is used when you use `IAbpDaprClientFactory`. If you need that value in your application, you can inject `IDaprApiTokenProvider` and use its `GetDaprApiToken()` method. ### App API Token diff --git a/docs/zh-Hans/Dapr/Index.md b/docs/zh-Hans/Dapr/Index.md index 64a9451737..a5237cbb71 100644 --- a/docs/zh-Hans/Dapr/Index.md +++ b/docs/zh-Hans/Dapr/Index.md @@ -60,29 +60,6 @@ Configure(options => } ```` -### 注入DaprClient - -ABP 将 `DaprClient` 类注册到 [依赖注入](../Dependency-Injection.md) 系统中.因此,你可以在需要时注入并使用它: - -````csharp -public class MyService : ITransientDependency -{ - private readonly DaprClient _daprClient; - - public MyService(DaprClient daprClient) - { - _daprClient = daprClient; - } - - public async Task DoItAsync() - { - // TODO: Use the injected _daprClient object - } -} -```` - -注入 `DaprClient` 是在应用程序代码中使用它的推荐方法.当你注入它时,将使用 `IAbpDaprClientFactory` 服务创建它,这会在下一节中将进行说明. - ### IAbpDaprClientFactory `IAbpDaprClientFactory` 可用于创建 `DaprClient` 或 `HttpClient` 对象来执行对 Dapr 的操作.它使用 `AbpDaprOptions`,因此你可以配置设置. @@ -113,15 +90,14 @@ public class MyService : ITransientDependency }); // Create an HttpClient object - HttpClient httpClient = await _daprClientFactory - .CreateHttpClientAsync("target-app-id"); + HttpClient httpClient = await _daprClientFactory.CreateHttpClientAsync("target-app-id"); } } ```` `CreateHttpClientAsync` 方法还获取可选的 `daprEndpoint` 和 `daprApiToken` 参数. -> ABP使用`IAbpDaprClientFactory`创建Dapr客户端.你也可以在应用程序中使用Dapr API创建客户端对象.推荐使用`IAbpDaprClientFactory`,但不是必需的. +> 你可以在应用程序中使用Dapr API创建客户端对象.推荐使用`IAbpDaprClientFactory`,但不是必需的. ## C# API 客户端代理集成 @@ -412,7 +388,7 @@ Configure(options => } ```` -一旦你设置了它,它就会在你注入`DaprClient`或使用`IAbpDaprClientFactory`时使用.如果你需要在应用程序中使用该值,你可以注入`IDaprApiTokenProvider`并使用其`GetDaprApiToken()`方法. +一旦你设置了它,它就会在使用`IAbpDaprClientFactory`时使用.如果你需要在应用程序中使用该值,你可以注入`IDaprApiTokenProvider`并使用其`GetDaprApiToken()`方法. ### App API Token