gpt4 book ai didi

c# - Service Fabric 可靠集合 : serialization issue

转载 作者:太空狗 更新时间:2023-10-29 22:25:56 25 4
gpt4 key购买 nike

可靠集合(队列)值存储一些复杂类型SomeUnit

我已将其标记为 [DataContract] 并将其成员标记为 [DataMember] 并添加了 [KnownType(typeof(SomeUnit))] 属性也在它上面。

看起来现在 SomeUnit 序列化了,但是随后抛出了反序列化异常:

Element 'urn:ServiceFabric.Communication:item' contains data from a type that maps to the name 'http://schemas.datacontract.org/2004/07/RP.Core:SomeUnit'. The deserializer has no knowledge of any type that maps to this name. Consider using a DataContractResolver if you are using DataContractSerializer or add the type corresponding to 'SomeUnit' to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding it to the list of known types passed to the serializer.

我该如何解决?

最佳答案

由于您没有显示任何代码,我们只能猜测是什么原因造成的。

如果您使用具有通用项目类型的可靠队列,例如 SomeUnit 的基类,则会出现此问题。

// Using reliable collection with a base item type
IReliableQueue<BaseClass> myQueue = ...;

// Store derived item in the queue
SomeUnit myData = ...; // SomeUnit inherit from BaseClass
await myQueue.EnqueueAsync(txn, myData); // OK to store but won't deserialize!

该队列的反序列化器知道如何解析 BaseClass,但它不会隐含地知道您的派生类 SomeUnit

您可以通过在基类上应用 KnownTypeAttribute 来解决此问题,从而显式声明反序列化程序应注意的派生类。

[DataContract]
[KnownType(typeof(SomeUnit))]
public class BaseClass
{
...
}

[DataContract]
public class SomeUnit : BaseClass
{
...
}

无法在接口(interface)类型上应用 [KnownType]。但是,有一些选项可以支持这一点:

选项 #1

使用包装合约来声明已知类型。

[DataContract]
[KnownType(typeof(SomeUnit))]
public class Wrapper
{
[DataMember]
public IUnit Value { get; set; }
}

[DataContract]
public class SomeUnit : IUnit
{
...
}

选项 #2

将已知类型指定给 DataContractSerializer constructor .

然而,这将要求您 tell service fabric to use your custom serializer .

选项#3

在配置文件 (app.config) 中将已知类型指定为 described here .

关于c# - Service Fabric 可靠集合 : serialization issue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34170177/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com