From 6faa7746166065dffff7447f1a147861ef62c5d0 Mon Sep 17 00:00:00 2001
From: cKey <35512826+colinin@users.noreply.github.com>
Date: Tue, 13 Apr 2021 11:41:09 +0800
Subject: [PATCH] LINGYUN.Abp.Dapr.Client fully compatible with
Volo.Abp.Http.Client
---
.../Abp/Dapr/Client/AbpDaprClientModule.cs | 5 +++
.../IRemoteServiceDaprClientAuthenticator.cs | 9 ----
...ullRemoteServiceDaprClientAuthenticator.cs | 14 ------
...oteServiceDaprClientAuthenticateContext.cs | 21 ---------
.../Abp/Dapr/Client/DaprClientFactory.cs | 45 -------------------
.../Client/DaprRemoteServiceConfiguration.cs | 15 ++-----
.../DynamicDaprClientProxyInterceptor.cs | 21 ++++++---
.../Abp/Dapr/Client/IDaprClientFactory.cs | 9 ----
...lectionDynamicDaprClientProxyExtensions.cs | 30 ++++++++++++-
9 files changed, 53 insertions(+), 116 deletions(-)
delete mode 100644 aspnet-core/modules/dapr/LINGYUN.Abp.Dapr.Client/LINGYUN/Abp/Dapr/Client/Authentication/IRemoteServiceDaprClientAuthenticator.cs
delete mode 100644 aspnet-core/modules/dapr/LINGYUN.Abp.Dapr.Client/LINGYUN/Abp/Dapr/Client/Authentication/NullRemoteServiceDaprClientAuthenticator.cs
delete mode 100644 aspnet-core/modules/dapr/LINGYUN.Abp.Dapr.Client/LINGYUN/Abp/Dapr/Client/Authentication/RemoteServiceDaprClientAuthenticateContext.cs
delete mode 100644 aspnet-core/modules/dapr/LINGYUN.Abp.Dapr.Client/LINGYUN/Abp/Dapr/Client/DaprClientFactory.cs
delete mode 100644 aspnet-core/modules/dapr/LINGYUN.Abp.Dapr.Client/LINGYUN/Abp/Dapr/Client/IDaprClientFactory.cs
diff --git a/aspnet-core/modules/dapr/LINGYUN.Abp.Dapr.Client/LINGYUN/Abp/Dapr/Client/AbpDaprClientModule.cs b/aspnet-core/modules/dapr/LINGYUN.Abp.Dapr.Client/LINGYUN/Abp/Dapr/Client/AbpDaprClientModule.cs
index 9fb43a63d..89b05d2fc 100644
--- a/aspnet-core/modules/dapr/LINGYUN.Abp.Dapr.Client/LINGYUN/Abp/Dapr/Client/AbpDaprClientModule.cs
+++ b/aspnet-core/modules/dapr/LINGYUN.Abp.Dapr.Client/LINGYUN/Abp/Dapr/Client/AbpDaprClientModule.cs
@@ -8,6 +8,11 @@ namespace LINGYUN.Abp.Dapr.Client
typeof(AbpHttpClientModule))]
public class AbpDaprClientModule : AbpModule
{
+ ///
+ /// 与AbpHttpClient集成,创建一个命名HttpClient
+ ///
+ internal const string DaprHttpClient = "_AbpDaprClient";
+
public override void ConfigureServices(ServiceConfigurationContext context)
{
var configuration = context.Services.GetConfiguration();
diff --git a/aspnet-core/modules/dapr/LINGYUN.Abp.Dapr.Client/LINGYUN/Abp/Dapr/Client/Authentication/IRemoteServiceDaprClientAuthenticator.cs b/aspnet-core/modules/dapr/LINGYUN.Abp.Dapr.Client/LINGYUN/Abp/Dapr/Client/Authentication/IRemoteServiceDaprClientAuthenticator.cs
deleted file mode 100644
index 822544406..000000000
--- a/aspnet-core/modules/dapr/LINGYUN.Abp.Dapr.Client/LINGYUN/Abp/Dapr/Client/Authentication/IRemoteServiceDaprClientAuthenticator.cs
+++ /dev/null
@@ -1,9 +0,0 @@
-using System.Threading.Tasks;
-
-namespace LINGYUN.Abp.Dapr.Client.Authentication
-{
- public interface IRemoteServiceDaprClientAuthenticator
- {
- Task AuthenticateAsync(RemoteServiceDaprClientAuthenticateContext context);
- }
-}
diff --git a/aspnet-core/modules/dapr/LINGYUN.Abp.Dapr.Client/LINGYUN/Abp/Dapr/Client/Authentication/NullRemoteServiceDaprClientAuthenticator.cs b/aspnet-core/modules/dapr/LINGYUN.Abp.Dapr.Client/LINGYUN/Abp/Dapr/Client/Authentication/NullRemoteServiceDaprClientAuthenticator.cs
deleted file mode 100644
index 6bdad8d7c..000000000
--- a/aspnet-core/modules/dapr/LINGYUN.Abp.Dapr.Client/LINGYUN/Abp/Dapr/Client/Authentication/NullRemoteServiceDaprClientAuthenticator.cs
+++ /dev/null
@@ -1,14 +0,0 @@
-using System.Threading.Tasks;
-using Volo.Abp.DependencyInjection;
-
-namespace LINGYUN.Abp.Dapr.Client.Authentication
-{
- [Dependency(TryRegister = true)]
- public class NullRemoteServiceDaprClientAuthenticator : IRemoteServiceDaprClientAuthenticator, ISingletonDependency
- {
- public Task AuthenticateAsync(RemoteServiceDaprClientAuthenticateContext context)
- {
- return Task.CompletedTask;
- }
- }
-}
diff --git a/aspnet-core/modules/dapr/LINGYUN.Abp.Dapr.Client/LINGYUN/Abp/Dapr/Client/Authentication/RemoteServiceDaprClientAuthenticateContext.cs b/aspnet-core/modules/dapr/LINGYUN.Abp.Dapr.Client/LINGYUN/Abp/Dapr/Client/Authentication/RemoteServiceDaprClientAuthenticateContext.cs
deleted file mode 100644
index a4225fb07..000000000
--- a/aspnet-core/modules/dapr/LINGYUN.Abp.Dapr.Client/LINGYUN/Abp/Dapr/Client/Authentication/RemoteServiceDaprClientAuthenticateContext.cs
+++ /dev/null
@@ -1,21 +0,0 @@
-using System.Net.Http;
-
-namespace LINGYUN.Abp.Dapr.Client.Authentication
-{
- public class RemoteServiceDaprClientAuthenticateContext
- {
- public HttpRequestMessage Request { get; }
- public DaprRemoteServiceConfiguration RemoteService { get; }
-
- public string RemoteServiceName { get; }
- public RemoteServiceDaprClientAuthenticateContext(
- HttpRequestMessage request,
- DaprRemoteServiceConfiguration remoteService,
- string remoteServiceName)
- {
- Request = request;
- RemoteService = remoteService;
- RemoteServiceName = remoteServiceName;
- }
- }
-}
diff --git a/aspnet-core/modules/dapr/LINGYUN.Abp.Dapr.Client/LINGYUN/Abp/Dapr/Client/DaprClientFactory.cs b/aspnet-core/modules/dapr/LINGYUN.Abp.Dapr.Client/LINGYUN/Abp/Dapr/Client/DaprClientFactory.cs
deleted file mode 100644
index 9e7c29b39..000000000
--- a/aspnet-core/modules/dapr/LINGYUN.Abp.Dapr.Client/LINGYUN/Abp/Dapr/Client/DaprClientFactory.cs
+++ /dev/null
@@ -1,45 +0,0 @@
-using Dapr.Client;
-using Microsoft.Extensions.Options;
-using System;
-using Volo.Abp.DependencyInjection;
-using Volo.Abp.Json.SystemTextJson;
-
-namespace LINGYUN.Abp.Dapr.Client
-{
- public class DaprClientFactory : IDaprClientFactory, ISingletonDependency
- {
- protected AbpDaprClientOptions DaprClientOptions { get; }
- protected AbpSystemTextJsonSerializerOptions JsonSerializerOptions { get; }
-
- private readonly Lazy _daprClientLazy;
-
- public DaprClientFactory(
- IOptions daprClientOptions,
- IOptions jsonSerializarOptions)
- {
- DaprClientOptions = daprClientOptions.Value;
- JsonSerializerOptions = jsonSerializarOptions.Value;
-
- _daprClientLazy = new Lazy(() => CreateDaprClient());
- }
-
- public DaprClient Create() => _daprClientLazy.Value;
-
- protected virtual DaprClient CreateDaprClient()
- {
- var builder = new DaprClientBuilder()
- .UseHttpEndpoint(DaprClientOptions.HttpEndpoint)
- .UseJsonSerializationOptions(JsonSerializerOptions.JsonSerializerOptions);
-
- if (!DaprClientOptions.GrpcEndpoint.IsNullOrWhiteSpace() &&
- DaprClientOptions.GrpcChannelOptions != null)
- {
- builder
- .UseGrpcEndpoint(DaprClientOptions.GrpcEndpoint)
- .UseGrpcChannelOptions(DaprClientOptions.GrpcChannelOptions);
- }
-
- return builder.Build();
- }
- }
-}
diff --git a/aspnet-core/modules/dapr/LINGYUN.Abp.Dapr.Client/LINGYUN/Abp/Dapr/Client/DaprRemoteServiceConfiguration.cs b/aspnet-core/modules/dapr/LINGYUN.Abp.Dapr.Client/LINGYUN/Abp/Dapr/Client/DaprRemoteServiceConfiguration.cs
index 62658c311..fbc680c7c 100644
--- a/aspnet-core/modules/dapr/LINGYUN.Abp.Dapr.Client/LINGYUN/Abp/Dapr/Client/DaprRemoteServiceConfiguration.cs
+++ b/aspnet-core/modules/dapr/LINGYUN.Abp.Dapr.Client/LINGYUN/Abp/Dapr/Client/DaprRemoteServiceConfiguration.cs
@@ -1,8 +1,9 @@
using System.Collections.Generic;
+using Volo.Abp.Http.Client;
namespace LINGYUN.Abp.Dapr.Client
{
- public class DaprRemoteServiceConfiguration : Dictionary
+ public class DaprRemoteServiceConfiguration : RemoteServiceConfiguration
{
///
/// Base AppId.
@@ -13,25 +14,17 @@ namespace LINGYUN.Abp.Dapr.Client
set => this[nameof(AppId)] = value;
}
- ///
- /// Version.
- ///
- public string Version
- {
- get => this.GetOrDefault(nameof(Version));
- set => this[nameof(Version)] = value;
- }
-
public DaprRemoteServiceConfiguration()
{
}
public DaprRemoteServiceConfiguration(
string appId,
+ string baseUil,
string version)
+ : base(baseUil, version)
{
this[nameof(AppId)] = appId;
- this[nameof(Version)] = version;
}
}
}
diff --git a/aspnet-core/modules/dapr/LINGYUN.Abp.Dapr.Client/LINGYUN/Abp/Dapr/Client/DynamicProxying/DynamicDaprClientProxyInterceptor.cs b/aspnet-core/modules/dapr/LINGYUN.Abp.Dapr.Client/LINGYUN/Abp/Dapr/Client/DynamicProxying/DynamicDaprClientProxyInterceptor.cs
index 9671108a0..d1401450a 100644
--- a/aspnet-core/modules/dapr/LINGYUN.Abp.Dapr.Client/LINGYUN/Abp/Dapr/Client/DynamicProxying/DynamicDaprClientProxyInterceptor.cs
+++ b/aspnet-core/modules/dapr/LINGYUN.Abp.Dapr.Client/LINGYUN/Abp/Dapr/Client/DynamicProxying/DynamicDaprClientProxyInterceptor.cs
@@ -1,5 +1,4 @@
using Dapr.Client;
-using LINGYUN.Abp.Dapr.Client.Authentication;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
@@ -18,6 +17,7 @@ using Volo.Abp.DependencyInjection;
using Volo.Abp.DynamicProxy;
using Volo.Abp.Http;
using Volo.Abp.Http.Client;
+using Volo.Abp.Http.Client.Authentication;
using Volo.Abp.Http.Client.DynamicProxying;
using Volo.Abp.Http.Modeling;
using Volo.Abp.Http.ProxyScripting.Generators;
@@ -37,11 +37,12 @@ namespace LINGYUN.Abp.Dapr.Client.DynamicProxying
protected ICorrelationIdProvider CorrelationIdProvider { get; }
protected ICurrentTenant CurrentTenant { get; }
protected AbpCorrelationIdOptions AbpCorrelationIdOptions { get; }
+ protected IDynamicProxyHttpClientFactory HttpClientFactory { get; }
protected IDaprApiDescriptionFinder ApiDescriptionFinder { get; }
protected AbpDaprRemoteServiceOptions AbpRemoteServiceOptions { get; }
protected AbpDaprClientProxyOptions ClientProxyOptions { get; }
protected IJsonSerializer JsonSerializer { get; }
- protected IRemoteServiceDaprClientAuthenticator ClientAuthenticator { get; }
+ protected IRemoteServiceHttpClientAuthenticator ClientAuthenticator { get; }
public ILogger> Logger { get; set; }
@@ -58,7 +59,8 @@ namespace LINGYUN.Abp.Dapr.Client.DynamicProxying
IOptionsSnapshot remoteServiceOptions,
IDaprApiDescriptionFinder apiDescriptionFinder,
IJsonSerializer jsonSerializer,
- IRemoteServiceDaprClientAuthenticator clientAuthenticator,
+ IDynamicProxyHttpClientFactory dynamicProxyHttpClientFactory,
+ IRemoteServiceHttpClientAuthenticator clientAuthenticator,
ICancellationTokenProvider cancellationTokenProvider,
ICorrelationIdProvider correlationIdProvider,
IOptions correlationIdOptions,
@@ -68,6 +70,7 @@ namespace LINGYUN.Abp.Dapr.Client.DynamicProxying
CancellationTokenProvider = cancellationTokenProvider;
CorrelationIdProvider = correlationIdProvider;
CurrentTenant = currentTenant;
+ HttpClientFactory = dynamicProxyHttpClientFactory;
AbpCorrelationIdOptions = correlationIdOptions.Value;
ApiDescriptionFinder = apiDescriptionFinder;
JsonSerializer = jsonSerializer;
@@ -162,13 +165,21 @@ namespace LINGYUN.Abp.Dapr.Client.DynamicProxying
AddHeaders(invocation, action, requestMessage, apiVersion);
- await ClientAuthenticator.AuthenticateAsync(
- new RemoteServiceDaprClientAuthenticateContext(
+ var client = HttpClientFactory.Create(AbpDaprClientModule.DaprHttpClient);
+ await ClientAuthenticator.Authenticate(
+ new RemoteServiceHttpClientAuthenticateContext(
+ client,
requestMessage,
remoteServiceConfig,
clientConfig.RemoteServiceName
)
);
+ // 其他库可能将授权标头写入到HttpClient中
+ if (requestMessage.Headers.Authorization == null &&
+ client.DefaultRequestHeaders.Authorization != null)
+ {
+ requestMessage.Headers.Authorization = client.DefaultRequestHeaders.Authorization;
+ }
var response = await DaprClient.InvokeMethodWithResponseAsync(requestMessage, GetCancellationToken());
diff --git a/aspnet-core/modules/dapr/LINGYUN.Abp.Dapr.Client/LINGYUN/Abp/Dapr/Client/IDaprClientFactory.cs b/aspnet-core/modules/dapr/LINGYUN.Abp.Dapr.Client/LINGYUN/Abp/Dapr/Client/IDaprClientFactory.cs
deleted file mode 100644
index 5eb8b3177..000000000
--- a/aspnet-core/modules/dapr/LINGYUN.Abp.Dapr.Client/LINGYUN/Abp/Dapr/Client/IDaprClientFactory.cs
+++ /dev/null
@@ -1,9 +0,0 @@
-using Dapr.Client;
-
-namespace LINGYUN.Abp.Dapr.Client
-{
- public interface IDaprClientFactory
- {
- DaprClient Create();
- }
-}
diff --git a/aspnet-core/modules/dapr/LINGYUN.Abp.Dapr.Client/Microsoft/Extensions/DependencyInjection/ServiceCollectionDynamicDaprClientProxyExtensions.cs b/aspnet-core/modules/dapr/LINGYUN.Abp.Dapr.Client/Microsoft/Extensions/DependencyInjection/ServiceCollectionDynamicDaprClientProxyExtensions.cs
index d323e2c1c..a6d8e610b 100644
--- a/aspnet-core/modules/dapr/LINGYUN.Abp.Dapr.Client/Microsoft/Extensions/DependencyInjection/ServiceCollectionDynamicDaprClientProxyExtensions.cs
+++ b/aspnet-core/modules/dapr/LINGYUN.Abp.Dapr.Client/Microsoft/Extensions/DependencyInjection/ServiceCollectionDynamicDaprClientProxyExtensions.cs
@@ -1,13 +1,16 @@
using Castle.DynamicProxy;
+using Dapr.Client;
using JetBrains.Annotations;
using LINGYUN.Abp.Dapr.Client;
using LINGYUN.Abp.Dapr.Client.DynamicProxying;
using Microsoft.Extensions.DependencyInjection.Extensions;
+using Microsoft.Extensions.Options;
using System;
using System.Linq;
using System.Reflection;
using Volo.Abp;
using Volo.Abp.Castle.DynamicProxy;
+using Volo.Abp.Json.SystemTextJson;
using Volo.Abp.Validation;
namespace Microsoft.Extensions.DependencyInjection
@@ -17,11 +20,34 @@ namespace Microsoft.Extensions.DependencyInjection
private static readonly ProxyGenerator ProxyGeneratorInstance = new ProxyGenerator();
public static IServiceCollection AddDaprClient(
- [NotNull] this IServiceCollection services)
+ [NotNull] this IServiceCollection services,
+ Action setup = null)
{
Check.NotNull(services, nameof(services));
- services.TryAddSingleton(provider => provider.GetRequiredService().Create());
+ services.TryAddSingleton(provider =>
+ {
+ var abpSystemTextJsonSerializerOptions = provider.GetRequiredService>().Value;
+ var abpDaprClientOptions = provider.GetRequiredService>().Value;
+
+ var builder = new DaprClientBuilder()
+ .UseHttpEndpoint(abpDaprClientOptions.HttpEndpoint)
+ .UseJsonSerializationOptions(abpSystemTextJsonSerializerOptions.JsonSerializerOptions);
+
+ if (!abpDaprClientOptions.GrpcEndpoint.IsNullOrWhiteSpace() &&
+ abpDaprClientOptions.GrpcChannelOptions != null)
+ {
+ builder
+ .UseGrpcEndpoint(abpDaprClientOptions.GrpcEndpoint)
+ .UseGrpcChannelOptions(abpDaprClientOptions.GrpcChannelOptions);
+ }
+
+ setup?.Invoke(builder);
+
+ return builder.Build();
+ });
+
+ services.AddHttpClient(AbpDaprClientModule.DaprHttpClient);
return services;
}