WCF 对象类型格式化程序错误



我在WCF服务中使用下面的数据协定,当我的客户端是C#控制台应用程序时,它可以工作

[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 中使用类型对象吗?

我没有尝试过,但我认为你不能。如果实体是对象类型,则无法明确指定协定,因为您无法按预期提前知道类型。

对于SoapUI,它不知道object中可能有哪些类型。您必须定义已知类型,以便模式 (WSDL) 将它们公开给 SoapUI。查看此 MSDN 文章

你应该有类似的东西

[DataContract]
[KnownType(typeof(Type1))]
[KnownType(typeof(Type2))]
public class PersonField { ... }

相关内容

最新更新