From 000f32e6b45d5588d2a6c69b7595702710b2300f Mon Sep 17 00:00:00 2001 From: maliming Date: Mon, 8 Aug 2022 14:21:57 +0800 Subject: [PATCH] Use abp JsonSerializerOptions in `AbpDaprClientFactory`. --- .../Volo/Abp/Dapr/AbpDaprClientFactory.cs | 33 +++++++++++++++---- 1 file changed, 26 insertions(+), 7 deletions(-) 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 2237547155..1cd37ceb94 100644 --- a/framework/src/Volo.Abp.Dapr/Volo/Abp/Dapr/AbpDaprClientFactory.cs +++ b/framework/src/Volo.Abp.Dapr/Volo/Abp/Dapr/AbpDaprClientFactory.cs @@ -1,25 +1,44 @@ -using Dapr.Client; +using System.Collections.Concurrent; +using System.Text.Json; +using Dapr.Client; using Microsoft.Extensions.Options; using Volo.Abp.DependencyInjection; +using Volo.Abp.Json.SystemTextJson; namespace Volo.Abp.Dapr; public class AbpDaprClientFactory : ITransientDependency { protected AbpDaprOptions Options { get; } + protected AbpSystemTextJsonSerializerOptions SystemTextJsonSerializerOptions { get; } - public AbpDaprClientFactory(IOptions options) + public AbpDaprClientFactory( + IOptions options, + IOptions systemTextJsonSerializerOptions) { Options = options.Value; + SystemTextJsonSerializerOptions = systemTextJsonSerializerOptions.Value; } - public virtual Task CreateAsync() + public virtual async Task CreateAsync() { - var daprClient = new DaprClientBuilder() + var builder = new DaprClientBuilder() .UseHttpEndpoint(Options.HttpEndpoint) - //.UseJsonSerializationOptions()//TODO: Use abp JsonSerializerOptions - .Build(); + .UseJsonSerializationOptions(await CreateJsonSerializerOptions()); - return Task.FromResult(daprClient); + if (!Options.GrpcEndpoint.IsNullOrWhiteSpace()) + { + builder.UseGrpcEndpoint(Options.GrpcEndpoint); + } + + return builder.Build(); + } + + private readonly static ConcurrentDictionary JsonSerializerOptionsCache = new ConcurrentDictionary(); + + protected virtual Task CreateJsonSerializerOptions() + { + return Task.FromResult(JsonSerializerOptionsCache.GetOrAdd(nameof(AbpDaprClientFactory), + _ => new JsonSerializerOptions(SystemTextJsonSerializerOptions.JsonSerializerOptions))); } }