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