1

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

4

2 回答 2

0

For SoapUI it does not know what types could be in object. You have to define the known types so that the schema (WSDL) exposes them to SoapUI. Have a look at this MSDN article

You should have something like

[DataContract]
[KnownType(typeof(Type1))]
[KnownType(typeof(Type2))]
public class PersonField { ... }
于 2012-05-08T23:53:00.590 回答
0

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

于 2012-05-08T23:52:11.277 回答