Browse Source

Merge pull request #248 from colinin/4.2

LINGYUN.Abp.Dapr.Client fully compatible with Volo.Abp.Http.Client
pull/252/head
cKey 5 years ago
committed by GitHub
parent
commit
b62287bafd
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      aspnet-core/modules/dapr/LINGYUN.Abp.Dapr.Client/LINGYUN/Abp/Dapr/Client/AbpDaprClientModule.cs
  2. 9
      aspnet-core/modules/dapr/LINGYUN.Abp.Dapr.Client/LINGYUN/Abp/Dapr/Client/Authentication/IRemoteServiceDaprClientAuthenticator.cs
  3. 14
      aspnet-core/modules/dapr/LINGYUN.Abp.Dapr.Client/LINGYUN/Abp/Dapr/Client/Authentication/NullRemoteServiceDaprClientAuthenticator.cs
  4. 21
      aspnet-core/modules/dapr/LINGYUN.Abp.Dapr.Client/LINGYUN/Abp/Dapr/Client/Authentication/RemoteServiceDaprClientAuthenticateContext.cs
  5. 45
      aspnet-core/modules/dapr/LINGYUN.Abp.Dapr.Client/LINGYUN/Abp/Dapr/Client/DaprClientFactory.cs
  6. 15
      aspnet-core/modules/dapr/LINGYUN.Abp.Dapr.Client/LINGYUN/Abp/Dapr/Client/DaprRemoteServiceConfiguration.cs
  7. 21
      aspnet-core/modules/dapr/LINGYUN.Abp.Dapr.Client/LINGYUN/Abp/Dapr/Client/DynamicProxying/DynamicDaprClientProxyInterceptor.cs
  8. 9
      aspnet-core/modules/dapr/LINGYUN.Abp.Dapr.Client/LINGYUN/Abp/Dapr/Client/IDaprClientFactory.cs
  9. 30
      aspnet-core/modules/dapr/LINGYUN.Abp.Dapr.Client/Microsoft/Extensions/DependencyInjection/ServiceCollectionDynamicDaprClientProxyExtensions.cs

5
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))] typeof(AbpHttpClientModule))]
public class AbpDaprClientModule : AbpModule public class AbpDaprClientModule : AbpModule
{ {
/// <summary>
/// 与AbpHttpClient集成,创建一个命名HttpClient
/// </summary>
internal const string DaprHttpClient = "_AbpDaprClient";
public override void ConfigureServices(ServiceConfigurationContext context) public override void ConfigureServices(ServiceConfigurationContext context)
{ {
var configuration = context.Services.GetConfiguration(); var configuration = context.Services.GetConfiguration();

9
aspnet-core/modules/dapr/LINGYUN.Abp.Dapr.Client/LINGYUN/Abp/Dapr/Client/Authentication/IRemoteServiceDaprClientAuthenticator.cs

@ -1,9 +0,0 @@
using System.Threading.Tasks;
namespace LINGYUN.Abp.Dapr.Client.Authentication
{
public interface IRemoteServiceDaprClientAuthenticator
{
Task AuthenticateAsync(RemoteServiceDaprClientAuthenticateContext context);
}
}

14
aspnet-core/modules/dapr/LINGYUN.Abp.Dapr.Client/LINGYUN/Abp/Dapr/Client/Authentication/NullRemoteServiceDaprClientAuthenticator.cs

@ -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;
}
}
}

21
aspnet-core/modules/dapr/LINGYUN.Abp.Dapr.Client/LINGYUN/Abp/Dapr/Client/Authentication/RemoteServiceDaprClientAuthenticateContext.cs

@ -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;
}
}
}

45
aspnet-core/modules/dapr/LINGYUN.Abp.Dapr.Client/LINGYUN/Abp/Dapr/Client/DaprClientFactory.cs

@ -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<DaprClient> _daprClientLazy;
public DaprClientFactory(
IOptions<AbpDaprClientOptions> daprClientOptions,
IOptions<AbpSystemTextJsonSerializerOptions> jsonSerializarOptions)
{
DaprClientOptions = daprClientOptions.Value;
JsonSerializerOptions = jsonSerializarOptions.Value;
_daprClientLazy = new Lazy<DaprClient>(() => 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();
}
}
}

15
aspnet-core/modules/dapr/LINGYUN.Abp.Dapr.Client/LINGYUN/Abp/Dapr/Client/DaprRemoteServiceConfiguration.cs

@ -1,8 +1,9 @@
using System.Collections.Generic; using System.Collections.Generic;
using Volo.Abp.Http.Client;
namespace LINGYUN.Abp.Dapr.Client namespace LINGYUN.Abp.Dapr.Client
{ {
public class DaprRemoteServiceConfiguration : Dictionary<string, string> public class DaprRemoteServiceConfiguration : RemoteServiceConfiguration
{ {
/// <summary> /// <summary>
/// Base AppId. /// Base AppId.
@ -13,25 +14,17 @@ namespace LINGYUN.Abp.Dapr.Client
set => this[nameof(AppId)] = value; set => this[nameof(AppId)] = value;
} }
/// <summary>
/// Version.
/// </summary>
public string Version
{
get => this.GetOrDefault(nameof(Version));
set => this[nameof(Version)] = value;
}
public DaprRemoteServiceConfiguration() public DaprRemoteServiceConfiguration()
{ {
} }
public DaprRemoteServiceConfiguration( public DaprRemoteServiceConfiguration(
string appId, string appId,
string baseUil,
string version) string version)
: base(baseUil, version)
{ {
this[nameof(AppId)] = appId; this[nameof(AppId)] = appId;
this[nameof(Version)] = version;
} }
} }
} }

21
aspnet-core/modules/dapr/LINGYUN.Abp.Dapr.Client/LINGYUN/Abp/Dapr/Client/DynamicProxying/DynamicDaprClientProxyInterceptor.cs

@ -1,5 +1,4 @@
using Dapr.Client; using Dapr.Client;
using LINGYUN.Abp.Dapr.Client.Authentication;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
@ -18,6 +17,7 @@ using Volo.Abp.DependencyInjection;
using Volo.Abp.DynamicProxy; using Volo.Abp.DynamicProxy;
using Volo.Abp.Http; using Volo.Abp.Http;
using Volo.Abp.Http.Client; using Volo.Abp.Http.Client;
using Volo.Abp.Http.Client.Authentication;
using Volo.Abp.Http.Client.DynamicProxying; using Volo.Abp.Http.Client.DynamicProxying;
using Volo.Abp.Http.Modeling; using Volo.Abp.Http.Modeling;
using Volo.Abp.Http.ProxyScripting.Generators; using Volo.Abp.Http.ProxyScripting.Generators;
@ -37,11 +37,12 @@ namespace LINGYUN.Abp.Dapr.Client.DynamicProxying
protected ICorrelationIdProvider CorrelationIdProvider { get; } protected ICorrelationIdProvider CorrelationIdProvider { get; }
protected ICurrentTenant CurrentTenant { get; } protected ICurrentTenant CurrentTenant { get; }
protected AbpCorrelationIdOptions AbpCorrelationIdOptions { get; } protected AbpCorrelationIdOptions AbpCorrelationIdOptions { get; }
protected IDynamicProxyHttpClientFactory HttpClientFactory { get; }
protected IDaprApiDescriptionFinder ApiDescriptionFinder { get; } protected IDaprApiDescriptionFinder ApiDescriptionFinder { get; }
protected AbpDaprRemoteServiceOptions AbpRemoteServiceOptions { get; } protected AbpDaprRemoteServiceOptions AbpRemoteServiceOptions { get; }
protected AbpDaprClientProxyOptions ClientProxyOptions { get; } protected AbpDaprClientProxyOptions ClientProxyOptions { get; }
protected IJsonSerializer JsonSerializer { get; } protected IJsonSerializer JsonSerializer { get; }
protected IRemoteServiceDaprClientAuthenticator ClientAuthenticator { get; } protected IRemoteServiceHttpClientAuthenticator ClientAuthenticator { get; }
public ILogger<DynamicDaprClientProxyInterceptor<TService>> Logger { get; set; } public ILogger<DynamicDaprClientProxyInterceptor<TService>> Logger { get; set; }
@ -58,7 +59,8 @@ namespace LINGYUN.Abp.Dapr.Client.DynamicProxying
IOptionsSnapshot<AbpDaprRemoteServiceOptions> remoteServiceOptions, IOptionsSnapshot<AbpDaprRemoteServiceOptions> remoteServiceOptions,
IDaprApiDescriptionFinder apiDescriptionFinder, IDaprApiDescriptionFinder apiDescriptionFinder,
IJsonSerializer jsonSerializer, IJsonSerializer jsonSerializer,
IRemoteServiceDaprClientAuthenticator clientAuthenticator, IDynamicProxyHttpClientFactory dynamicProxyHttpClientFactory,
IRemoteServiceHttpClientAuthenticator clientAuthenticator,
ICancellationTokenProvider cancellationTokenProvider, ICancellationTokenProvider cancellationTokenProvider,
ICorrelationIdProvider correlationIdProvider, ICorrelationIdProvider correlationIdProvider,
IOptions<AbpCorrelationIdOptions> correlationIdOptions, IOptions<AbpCorrelationIdOptions> correlationIdOptions,
@ -68,6 +70,7 @@ namespace LINGYUN.Abp.Dapr.Client.DynamicProxying
CancellationTokenProvider = cancellationTokenProvider; CancellationTokenProvider = cancellationTokenProvider;
CorrelationIdProvider = correlationIdProvider; CorrelationIdProvider = correlationIdProvider;
CurrentTenant = currentTenant; CurrentTenant = currentTenant;
HttpClientFactory = dynamicProxyHttpClientFactory;
AbpCorrelationIdOptions = correlationIdOptions.Value; AbpCorrelationIdOptions = correlationIdOptions.Value;
ApiDescriptionFinder = apiDescriptionFinder; ApiDescriptionFinder = apiDescriptionFinder;
JsonSerializer = jsonSerializer; JsonSerializer = jsonSerializer;
@ -162,13 +165,21 @@ namespace LINGYUN.Abp.Dapr.Client.DynamicProxying
AddHeaders(invocation, action, requestMessage, apiVersion); AddHeaders(invocation, action, requestMessage, apiVersion);
await ClientAuthenticator.AuthenticateAsync( var client = HttpClientFactory.Create(AbpDaprClientModule.DaprHttpClient);
new RemoteServiceDaprClientAuthenticateContext( await ClientAuthenticator.Authenticate(
new RemoteServiceHttpClientAuthenticateContext(
client,
requestMessage, requestMessage,
remoteServiceConfig, remoteServiceConfig,
clientConfig.RemoteServiceName 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()); var response = await DaprClient.InvokeMethodWithResponseAsync(requestMessage, GetCancellationToken());

9
aspnet-core/modules/dapr/LINGYUN.Abp.Dapr.Client/LINGYUN/Abp/Dapr/Client/IDaprClientFactory.cs

@ -1,9 +0,0 @@
using Dapr.Client;
namespace LINGYUN.Abp.Dapr.Client
{
public interface IDaprClientFactory
{
DaprClient Create();
}
}

30
aspnet-core/modules/dapr/LINGYUN.Abp.Dapr.Client/Microsoft/Extensions/DependencyInjection/ServiceCollectionDynamicDaprClientProxyExtensions.cs

@ -1,13 +1,16 @@
using Castle.DynamicProxy; using Castle.DynamicProxy;
using Dapr.Client;
using JetBrains.Annotations; using JetBrains.Annotations;
using LINGYUN.Abp.Dapr.Client; using LINGYUN.Abp.Dapr.Client;
using LINGYUN.Abp.Dapr.Client.DynamicProxying; using LINGYUN.Abp.Dapr.Client.DynamicProxying;
using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
using System; using System;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using Volo.Abp; using Volo.Abp;
using Volo.Abp.Castle.DynamicProxy; using Volo.Abp.Castle.DynamicProxy;
using Volo.Abp.Json.SystemTextJson;
using Volo.Abp.Validation; using Volo.Abp.Validation;
namespace Microsoft.Extensions.DependencyInjection namespace Microsoft.Extensions.DependencyInjection
@ -17,11 +20,34 @@ namespace Microsoft.Extensions.DependencyInjection
private static readonly ProxyGenerator ProxyGeneratorInstance = new ProxyGenerator(); private static readonly ProxyGenerator ProxyGeneratorInstance = new ProxyGenerator();
public static IServiceCollection AddDaprClient( public static IServiceCollection AddDaprClient(
[NotNull] this IServiceCollection services) [NotNull] this IServiceCollection services,
Action<DaprClientBuilder> setup = null)
{ {
Check.NotNull(services, nameof(services)); Check.NotNull(services, nameof(services));
services.TryAddSingleton(provider => provider.GetRequiredService<IDaprClientFactory>().Create()); services.TryAddSingleton(provider =>
{
var abpSystemTextJsonSerializerOptions = provider.GetRequiredService<IOptions<AbpSystemTextJsonSerializerOptions>>().Value;
var abpDaprClientOptions = provider.GetRequiredService<IOptions<AbpDaprClientOptions>>().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; return services;
} }

Loading…
Cancel
Save