WCF对象类型Formatter错误

时间:2012-05-08 23:24:03

标签: c# wcf web-services serialization soapui

我在我的WCF服务中使用下面的Data Contract,当我的客户端是C#console应用程序

时,它可以正常工作
[DataContract]
public class PersonField
{
    private string _fieldName;
    private object _fieldValue;

    public PersonField()
    {
    }

    public PersonField(string FieldName, object FieldValue)
    {
        _fieldName = FieldName;
        _fieldValue = FieldValue;
    }
    [DataMember]
    public string FieldName
    {
        get { return _fieldName; }
        set { _fieldName = value; }
    }
    [DataMember]
    public object FieldValue
    {
        get { return _fieldValue; }
        set { _fieldValue = value; }
    }
}

但是

当我从SOAP UI(http://www.soapui.org/)尝试时,我得到以下回复

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
   <s:Body>
      <s:Fault>
         <faultcode xmlns:a="http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher">a:DeserializationFailed</faultcode>
         <faultstring xml:lang="en-US">The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter entities. The InnerException message was 'Element FieldValue from namespace http://schemas.datacontract.org/2004/07/Entities.Person cannot have child contents to be deserialized as an object. Please use XmlNode[] to deserialize this pattern of XML.'.  Please see InnerException for more details.</faultstring>
      </s:Fault>
   </s:Body>
</s:Envelope>

我不能在WCF中使用类型对象吗?

2 个答案:

答案 0 :(得分:0)

我没有尝试,但我认为你不能。如果实体属于object类型,则无法明确指定合同,因为您无法按预期知道类型。

答案 1 :(得分:0)

对于SoapUI,它不知道object中可以包含哪些类型。您必须定义已知类型,以便模式(WSDL)将它们公开给SoapUI。看看this MSDN article

你应该有像

这样的东西
[DataContract]
[KnownType(typeof(Type1))]
[KnownType(typeof(Type2))]
public class PersonField { ... }