From 04b17e8a63937c7765dba82d4a46db7537135cab Mon Sep 17 00:00:00 2001 From: Mohammad Javad Ebrahimi Date: Wed, 18 Sep 2024 05:05:57 +0330 Subject: [PATCH] Bugfix #537 issue and Correct namespace and StringEncoding default value (#538) --- .../EasyCachingMemPackSerializerOptions.cs | 10 +++++--- .../EasyCachingOptionsExtensions.cs | 13 +++++------ .../MemoryPackOptionsExtension.cs | 23 +++++++++++-------- 3 files changed, 26 insertions(+), 20 deletions(-) diff --git a/serialization/EasyCaching.Serialization.MemoryPack/Configurations/EasyCachingMemPackSerializerOptions.cs b/serialization/EasyCaching.Serialization.MemoryPack/Configurations/EasyCachingMemPackSerializerOptions.cs index 033aa04d..e7748658 100644 --- a/serialization/EasyCaching.Serialization.MemoryPack/Configurations/EasyCachingMemPackSerializerOptions.cs +++ b/serialization/EasyCaching.Serialization.MemoryPack/Configurations/EasyCachingMemPackSerializerOptions.cs @@ -7,7 +7,11 @@ namespace EasyCaching.Serialization.MemoryPack; /// public record EasyCachingMemPackSerializerOptions { - public StringEncoding StringEncoding { set; get; } + /// + /// Gets or sets the string encoding. (Defaults to ) + /// + /// + /// The string encoding. + /// + public StringEncoding StringEncoding { set; get; } = StringEncoding.Utf8; } - - diff --git a/serialization/EasyCaching.Serialization.MemoryPack/Configurations/EasyCachingOptionsExtensions.cs b/serialization/EasyCaching.Serialization.MemoryPack/Configurations/EasyCachingOptionsExtensions.cs index 0585d747..2373b277 100644 --- a/serialization/EasyCaching.Serialization.MemoryPack/Configurations/EasyCachingOptionsExtensions.cs +++ b/serialization/EasyCaching.Serialization.MemoryPack/Configurations/EasyCachingOptionsExtensions.cs @@ -1,8 +1,7 @@ -using MemoryPack; -using EasyCaching.Core.Configurations; -using EasyCaching.Serialization.Json; +using EasyCaching.Core.Configurations; +using EasyCaching.Serialization.MemoryPack; -namespace EasyCaching.Serialization.MemoryPack; +namespace Microsoft.Extensions.DependencyInjection; /// /// Easy caching options extensions. @@ -13,7 +12,7 @@ public static class EasyCachingOptionsExtensions /// Withs the memory pack serializer. /// /// Options. - /// The name of this serializer instance. + /// The name of this serializer instance. public static EasyCachingOptions WithMemoryPack(this EasyCachingOptions options, string name = "mempack") { options.RegisterExtension(new MemoryPackOptionsExtension(name, null)); @@ -23,10 +22,10 @@ public static EasyCachingOptions WithMemoryPack(this EasyCachingOptions options, /// /// Withs the memory pack serializer. - /// + /// /// Options. /// Configure serializer settings. - /// The name of this serializer instance. + /// The name of this serializer instance. public static EasyCachingOptions WithMemoryPack(this EasyCachingOptions options, Action serializerOptions, string name) { options.RegisterExtension(new MemoryPackOptionsExtension(name, serializerOptions)); diff --git a/serialization/EasyCaching.Serialization.MemoryPack/Configurations/MemoryPackOptionsExtension.cs b/serialization/EasyCaching.Serialization.MemoryPack/Configurations/MemoryPackOptionsExtension.cs index 58126711..d0983f1a 100644 --- a/serialization/EasyCaching.Serialization.MemoryPack/Configurations/MemoryPackOptionsExtension.cs +++ b/serialization/EasyCaching.Serialization.MemoryPack/Configurations/MemoryPackOptionsExtension.cs @@ -1,12 +1,10 @@ -namespace EasyCaching.Serialization.Json; - -using System; using EasyCaching.Core.Configurations; using EasyCaching.Core.Serialization; -using EasyCaching.Serialization.MemoryPack; -using global::MemoryPack; +using MemoryPack; using Microsoft.Extensions.DependencyInjection; +namespace EasyCaching.Serialization.MemoryPack; + /// /// MemoryPack options extension. /// @@ -29,8 +27,8 @@ internal sealed class MemoryPackOptionsExtension : IEasyCachingOptionsExtension /// Configure. public MemoryPackOptionsExtension(string name, Action configure) { - this._name = name; - this._configure = configure; + _name = name; + _configure = configure; } /// @@ -44,11 +42,16 @@ public void AddServices(IServiceCollection services) services.AddOptions(); services.Configure(_name, configure); - services.AddSingleton(x => + services.AddSingleton(services => { - var optionsMon = x.GetRequiredService>(); + var optionsMon = services.GetRequiredService>(); var easyCachingOptions = optionsMon.Get(_name); - var options = new MemoryPackSerializerOptions { StringEncoding = easyCachingOptions.StringEncoding }; + + var options = MemoryPackSerializerOptions.Default; + typeof(MemoryPackSerializerOptions) + .GetProperty(nameof(MemoryPackSerializerOptions.StringEncoding)) + .SetValue(options, easyCachingOptions.StringEncoding); + return new DefaultMemoryPackSerializer(_name, options); }); }