为什么不能WCF通过在字典的对象? [英] Why can't WCF pass an object in a dictionary?
问题描述
在我的WCF服务,我定义为KnownType和ServiceKnown类型的对象QualifiedNumber。如果我用QualifiedNumber以下方法:
In my WCF service I have the object QualifiedNumber defined as KnownType and ServiceKnown type. If I used the QualifiedNumber in the following methods:
这一个不工作。它抛出一个异常,在部分的内容:
This one does NOT work. It throws an exception that in part reads:
元素http://schemas.microsoft.com/2003/10/Serialization/Arrays:Value包含的数据http://schemas.datacontract.org/2004/07 ServiceLibrary.Web.Model: QualifiedNumber数据契约。解串器并不知道映射到本合同的任何类型的。 无法反序列化,因为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;
}
这是关于引起异常的字典吗?
What is it about the dictionary that causes the exception?
推荐答案
下面是在WCF对通用词典序列化的详细文章:
Here is a detailed article on generic dictionary serialization over WCF:
的http://www.request-response.com/blog/PermaLink,guid,ff5fab81-affb-4b2b-aa67-c80bdfc86cbd.aspx
这是那篇文章的外卖报价是:
The takeaway quote from that article would be:
有没有办法使用WSDL / XSD有意义地传达一个.NET字典类的语义。
There is no way to meaningfully convey the semantics of a .NET dictionary class using WSDL/XSD.
这篇关于为什么不能WCF通过在字典的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!