From 0320fc81320d6b6d4e202f730a36b962ae7c78b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Wed, 14 Sep 2022 10:20:59 +0300 Subject: [PATCH] No need to use ConcurrentDictionary since we only have a single item. --- .../Volo/Abp/Dapr/AbpDaprClientFactory.cs | 39 +++++++++---------- 1 file changed, 18 insertions(+), 21 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 597c3e74ce..ed04aa9b03 100644 --- a/framework/src/Volo.Abp.Dapr/Volo/Abp/Dapr/AbpDaprClientFactory.cs +++ b/framework/src/Volo.Abp.Dapr/Volo/Abp/Dapr/AbpDaprClientFactory.cs @@ -1,5 +1,4 @@ -using System.Collections.Concurrent; -using System.Text.Json; +using System.Text.Json; using Dapr.Client; using Microsoft.Extensions.Options; using Volo.Abp.DependencyInjection; @@ -7,41 +6,39 @@ using Volo.Abp.Json.SystemTextJson; namespace Volo.Abp.Dapr; -public class AbpDaprClientFactory : ITransientDependency, IAbpDaprClientFactory +public class AbpDaprClientFactory : IAbpDaprClientFactory, ISingletonDependency { - protected AbpDaprOptions Options { get; } - protected AbpSystemTextJsonSerializerOptions SystemTextJsonSerializerOptions { get; } - private readonly static ConcurrentDictionary JsonSerializerOptionsCache = new(); + protected AbpDaprOptions DaprOptions { get; } + protected JsonSerializerOptions JsonSerializerOptions { get; } public AbpDaprClientFactory( IOptions options, IOptions systemTextJsonSerializerOptions) { - Options = options.Value; - SystemTextJsonSerializerOptions = systemTextJsonSerializerOptions.Value; + DaprOptions = options.Value; + JsonSerializerOptions = CreateJsonSerializerOptions(systemTextJsonSerializerOptions.Value); } - public virtual async Task CreateAsync() + protected virtual JsonSerializerOptions CreateJsonSerializerOptions(AbpSystemTextJsonSerializerOptions systemTextJsonSerializerOptions) + { + return new JsonSerializerOptions(systemTextJsonSerializerOptions.JsonSerializerOptions); + } + + public virtual Task CreateAsync() { var builder = new DaprClientBuilder() - .UseJsonSerializationOptions(await CreateJsonSerializerOptions()); + .UseJsonSerializationOptions(JsonSerializerOptions); - if (!Options.HttpEndpoint.IsNullOrWhiteSpace()) + if (!DaprOptions.HttpEndpoint.IsNullOrWhiteSpace()) { - builder.UseHttpEndpoint(Options.HttpEndpoint); + builder.UseHttpEndpoint(DaprOptions.HttpEndpoint); } - if (!Options.GrpcEndpoint.IsNullOrWhiteSpace()) + if (!DaprOptions.GrpcEndpoint.IsNullOrWhiteSpace()) { - builder.UseGrpcEndpoint(Options.GrpcEndpoint); + builder.UseGrpcEndpoint(DaprOptions.GrpcEndpoint); } - return builder.Build(); - } - - protected virtual Task CreateJsonSerializerOptions() - { - return Task.FromResult(JsonSerializerOptionsCache.GetOrAdd(nameof(AbpDaprClientFactory), - _ => new JsonSerializerOptions(SystemTextJsonSerializerOptions.JsonSerializerOptions))); + return Task.FromResult(builder.Build()); } } \ No newline at end of file