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

Jer*_*Kur 5 .net c# wcf

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

这个不起作用.它引发了一个异常,部分内容如下:

元素' http://schemas.microsoft.com/2003/10/Serialization/Arrays:Value '包含' http://schemas.datacontract.org/2004/07 ServiceLibrary.Web.Model:QualifiedNumber'数据合同的数据.反序列化器不知道映射到此合同的任何类型.无法反序列化,因为QualifiedNumber的定义未知.

[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;
}
Run Code Online (Sandbox Code Playgroud)

这个工作

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;
}
Run Code Online (Sandbox Code Playgroud)

什么是导致异常的字典?

Dav*_*sky 4

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

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

该文章的要点是:

无法使用 WSDL/XSD 有意义地传达 .NET 字典类的语义。