首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不存储/返回NodaTime LocalDateTime的Akavache

不存储/返回NodaTime LocalDateTime的Akavache
EN

Stack Overflow用户
提问于 2019-06-12 12:34:29
回答 1查看 229关注 0票数 0

我需要将NodaTime LocalDateTime存储在Akavache缓存中。

我创建了一个简单的应用程序,它接受以下类并将其存储/从Akavache缓存中检索:

代码语言:javascript
复制
public class TestModel
{
        public string Name { get; set; }
        public LocalDateTime StartDateTimeLocal {get; set;}
        public DateTime StartDateTimeUtc {get;set;}
}

当它存储在缓存中并从缓存中检索时,还没有填充StartDateTimeLocal属性。

看来Akavache不知道如何序列化/反序列化LocalDateTime。

是否可以向Akavache注册类型或为未知类型提供自定义序列化?

控制台应用程序来演示它:

代码语言:javascript
复制
using Akavache;
using NodaTime;
using System;
using System.Reactive.Linq;

namespace AkavacheNodaTimeCore
{
    class Program
    {
        static TestModel BeforeModel;
        static TestModel AfterModel;
        static void Main(string[] args)
        {
            // Note that we're using Akavache 6.0.27, to match the version we're using in our live system.
            BlobCache.ApplicationName = "AkavacheNodaTimeCore";
            BlobCache.EnsureInitialized();

            BeforeModel = new TestModel()
            {
                StartLocalDateTime = LocalDateTime.FromDateTime(DateTime.Now),
                StartDateTime = DateTime.UtcNow,
            };

            Console.WriteLine($"Before:LocalDateTime='{BeforeModel.StartLocalDateTime}' DateTime='{BeforeModel.StartDateTime}'");

            CycleTheModels();

            Console.WriteLine($"After: LocalDateTime='{AfterModel.StartLocalDateTime}' DateTime='{AfterModel.StartDateTime}'");
            Console.WriteLine("Note that Akavache retrieves DateTimes as DateTimeKind.Local, so DateTime before and after above will differ.");

            Console.WriteLine("Press any key to continue.");
            var y = Console.ReadKey();

        }
        /// <summary>
        /// Puts a model into Akavache and retrieves a new one so we can compare.
        /// </summary>
        static async void CycleTheModels()
        {
            await BlobCache.InMemory.Invalidate("model");
            await BlobCache.InMemory.InsertObject("model", BeforeModel);

            AfterModel = await BlobCache.InMemory.GetObject<TestModel>("model");
        }
    }
}

TestModel类:

代码语言:javascript
复制
using NodaTime;
using System;

namespace AkavacheNodaTimeCore
{
    public class TestModel
    {
        public string Name { get; set; }
        public LocalDateTime StartLocalDateTime { get; set; }
        public DateTime StartDateTime {get;set;}

    }
}

我在控制台应用程序中添加了一个带有上述内容的Git回购,这说明了这个问题。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-13 09:24:55

您需要配置Akavache与JsonSerializerSettings一起使用的Json.NET,您需要一个对NodaTime.Serialization.JsonNet的引用,此时您可以创建一个序列化程序设置实例,为Noda配置它,然后在Splat中添加它作为一个依赖项( Akavache使用它)。我以前没有使用过Splat,所以这可能不是正确的方法,但它适用于您的示例:

代码语言:javascript
复制
using Newtonsoft.Json;
using NodaTime.Serialization.JsonNet;
using Splat;

...

// This should be before any of your other code.
var settings = new JsonSerializerSettings();
settings.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb);
Locator.CurrentMutable.RegisterConstant(settings, typeof(JsonSerializerSettings));

可能值得在Akavache回购中提交更多的文档,以便定制序列化设置--上面的工作,但只是猜测和一点源代码调查。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56562375

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档