gpt4 book ai didi

c# - 为什么 WCF 不能传递字典中的对象?

转载 作者:太空狗 更新时间:2023-10-29 23:18:16 24 4
gpt4 key购买 nike

在我的 WCF 服务中,我将对象 QualifiedNumber 定义为 KnownTypeServiceKnown 类型。如果我在以下方法中使用 QualifiedNumber:

这个不行。它抛出一个异常,部分内容如下:

Element 'http://schemas.microsoft.com/2003/10/Serialization/Arrays:Value' contains data of the 'http://schemas.datacontract.org/2004/07 ServiceLibrary.Web.Model:QualifiedNumber' data contract. The deserializer has no knowledge of any type that maps to this contract. cannot deserialize because the definition of QualifiedNumber is not known.

[OperationContract]
public Dictionary<int, object> TestDictionaryGet()
{
Dictionary<int, object> retDict = new Dictionary<int, object>();

retDict.Add(1, new QualifiedNumber(new decimal(1.2), "<"));
retDict.Add(2, "pass a simple string");

return retDict;
}

这个确实有效

public struct element
{
public int key;
public object value;
}

[OperationContract]
public List<element> TestElementListGet()
{
Dictionary<int, object> retDict = new Dictionary<int, object>();

retDict.Add(1, new QualifiedNumber(new decimal(1.2), "<"));
retDict.Add(2, "pass a simple string");

List<element> retElements = new List<element>();
foreach (KeyValuePair<int, object> item in retDict)
{
element newElement;
newElement.key = item.Key;
newElement.value = item.Value;

retElements.Add(newElement);
}

return retElements;
}

导致异常的字典是什么?

最佳答案

这是一篇关于通过 WCF 进行通用字典序列化的详细文章:

http://www.request-response.com/blog/PermaLink,guid,ff5fab81-affb-4b2b-aa67-c80bdfc86cbd.aspx

那篇文章的要点是:

There is no way to meaningfully convey the semantics of a .NET dictionary class using WSDL/XSD.

关于c# - 为什么 WCF 不能传递字典中的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5953808/

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