Browse Source

Merge pull request #18117 from abpframework/IAbpDaprClientFactory

Make `IAbpDaprClientFactory` async.
pull/18171/head
Halil İbrahim Kalkan 3 years ago
committed by GitHub
parent
commit
c0e225a009
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 30
      docs/en/Dapr/Index.md
  2. 30
      docs/zh-Hans/Dapr/Index.md
  3. 4
      framework/src/Volo.Abp.Dapr/Volo.Abp.Dapr.csproj
  4. 31
      framework/src/Volo.Abp.Dapr/Volo/Abp/Dapr/AbpDaprClientFactory.cs
  5. 14
      framework/src/Volo.Abp.Dapr/Volo/Abp/Dapr/AbpDaprModule.cs
  6. 5
      framework/src/Volo.Abp.Dapr/Volo/Abp/Dapr/IAbpDaprClientFactory.cs
  7. 2
      framework/src/Volo.Abp.DistributedLocking.Dapr/Volo/Abp/DistributedLocking/Dapr/DaprAbpDistributedLock.cs
  8. 2
      framework/src/Volo.Abp.EventBus.Dapr/Volo/Abp/EventBus/Dapr/DaprDistributedEventBus.cs

30
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

30
docs/zh-Hans/Dapr/Index.md

@ -60,29 +60,6 @@ Configure<AbpDaprOptions>(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<AbpDaprOptions>(options =>
}
````
一旦你设置了它,它就会在你注入`DaprClient`或使用`IAbpDaprClientFactory`时使用.如果你需要在应用程序中使用该值,你可以注入`IDaprApiTokenProvider`并使用其`GetDaprApiToken()`方法.
一旦你设置了它,它就会在使用`IAbpDaprClientFactory`时使用.如果你需要在应用程序中使用该值,你可以注入`IDaprApiTokenProvider`并使用其`GetDaprApiToken()`方法.
### App API Token

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

@ -11,12 +11,14 @@
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Volo.Abp.Http.Client\Volo.Abp.Http.Client.csproj" />
<ProjectReference Include="..\Volo.Abp.Json\Volo.Abp.Json.csproj" />
<ProjectReference Include="..\Volo.Abp.MultiTenancy.Abstractions\Volo.Abp.MultiTenancy.Abstractions.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Dapr.Client" />
<PackageReference Include="Dapr.Client" />
<PackageReference Include="IdentityModel" />
</ItemGroup>
</Project>

31
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> AbpCorrelationIdOptions { get; }
protected IRemoteServiceHttpClientAuthenticator RemoteServiceHttpClientAuthenticator { get; }
public AbpDaprClientFactory(
IOptions<AbpDaprOptions> options,
IOptions<AbpSystemTextJsonSerializerOptions> systemTextJsonSerializerOptions,
IDaprApiTokenProvider daprApiTokenProvider,
ICurrentTenant currentTenant, ICorrelationIdProvider correlationIdProvider,
IOptions<AbpCorrelationIdOptions> abpCorrelationIdOptions)
IOptions<AbpCorrelationIdOptions> 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<DaprClientBuilder>? builderAction = null)
public virtual Task<DaprClient> CreateAsync(Action<DaprClientBuilder>? 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<HttpClient> 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;
}

14
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<IAbpDaprClientFactory>()
.Create()
);
}
private void ConfigureDaprOptions(IConfiguration configuration)

5
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<DaprClientBuilder>? builderAction = null);
Task<DaprClient> CreateAsync(Action<DaprClientBuilder>? builderAction = null);
HttpClient CreateHttpClient(
Task<HttpClient> CreateHttpClientAsync(
string? appId = null,
string? daprEndpoint = null,
string? daprApiToken = null

2
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,

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

Loading…
Cancel
Save