简体   繁体   English

奥尔良私有财产反序列化

[英]Orleans Private Property Deserialization

I have an orleans project with a stateful grain.我有一个带有状态谷物的奥尔良项目。

Orleans seems to not deserialize private properties when hydrating the state for the grain. Orleans 在为 grain 补水 state 时似乎没有反序列化私有属性。

In the image below UserId gets deserialized but not State.在下图中,UserId 被反序列化,但不是 State。

Is there a way to get around this?有办法解决这个问题吗?

在此处输入图像描述

You can override the default GrainStorageSerializer to use System.Text.Json which allows properties with private setters to be deserialized when you apply the JsonInclude attribute to the property as shown below.您可以覆盖默认的 GrainStorageSerializer 以使用System.Text.Json ,当您将JsonInclude属性应用于属性时,它允许反序列化具有私有设置器的属性,如下所示。

public class Order
{
    [JsonInclude]
    public OrderStates State{ get; private set; }
}

You can then do the follow setup when configuring your environment.然后,您可以在配置环境时执行以下设置。 Implement a version of IGrainStorageSerializer that uses System.Text.Json .实现一个使用System.Text.JsonIGrainStorageSerializer版本。 In this case below I created SystemTextJsonSerializer which implements the interface.在下面的这种情况下,我创建了实现该接口的SystemTextJsonSerializer

public class SystemTextJsonSerializer : IGrainStorageSerializer
{
    public BinaryData Serialize<T>(T input)
    {
        return new BinaryData(JsonSerializer.SerializeToUtf8Bytes(input));
    }

    public T Deserialize<T>(BinaryData input)
    {
        return input.ToObjectFromJson<T>();
    }
}

You can then configure your GrainStorageSerializer as below.然后你可以配置你的GrainStorageSerializer如下。

siloBuilder.Services.AddSerializer(serializerBuilder =>
    {
        serializerBuilder.AddJsonSerializer(isSupported: type => type.Namespace.StartsWith("Example.Domain"));
    });
        
.AddAzureTableGrainStorage(
    name: "OrleansProvider",
    configureOptions: options =>
    {
        options.GrainStorageSerializer = new SystemTextJsonSerializer();
        options.ConfigureTableServiceClient("...");
    });
       

You can make the grain's state a separate serializable object with public properties and let Orleans inject it as an IPersistentState into your grain's constructor like explained in Grain persistence on Microsoft Learn in the Get started section.您可以使 grain 的 state 成为具有公共属性的单独的可序列化 object,并让 Orleans 将其作为 IPersistentState 注入到您的 grain 的构造函数中,如入门部分Microsoft Learn 上的 Grain 持久性中所述。

Keep in mind that you can't use the actual state data in the grain's constructor.请记住,您不能在 grain 的构造函数中使用实际的 state 数据。
Implement OnActivateAsync if you need to do extra housekeeping using state data before the first message gets dispatched to your grain.如果您需要在将第一条消息发送到您的 grain 之前使用 state 数据进行额外的内务处理,请实施OnActivateAsync

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM