我在DataContract中有一组定义为
的对象Collection<BaseAbstractObject>
我想填写一些 ClassA 类型的继承对象。
我正在使用Soap UI为我的服务方法创建此类请求,但我会收到错误 &#34;无法创建抽象类&#34;
我还使用 [KnownType(ClassA)] 属性修饰了基类。我的xml请求如下所示:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/" xmlns:wcf="http://schemas.datacontract.org/2004/07/WcfService1" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Header/>
<soapenv:Body>
<tem:GetDataUsingDataContract>
<tem:composite>
<wcf:ClassesCollection>
<wcf:BaseClass i:type="ClassA" >
<wcf:Id>1</wcf:Id>
<wcf:Name>ClassA</wcf:Name>
</wcf:BaseClass>
</wcf:ClassesCollection>
</tem:composite>
</tem:GetDataUsingDataContract>
</soapenv:Body>
</soapenv:Envelope>
这就是我的DataContracts的样子:
[DataContract]
public class MyDataContract
{
public MyDataContract()
{
ClassesCollection = new Collection<BaseClass>();
}
[DataMember]
public Collection<BaseClass> ClassesCollection { get; private set; }
}
[DataContract]
[KnownType(typeof(ClassA))]
public abstract class BaseClass
{
[DataMember]
public int Id { get; set; }
}
[DataContract]
public class ClassA : BaseClass
{
[DataMember]
public int Name { get; set; }
}
那么我的要求应该是什么样的?或者我需要向DataContract添加一些其他属性吗?
答案 0 :(得分:0)
首先确保您的集合具有公共setter,否则WCF将无法对其进行反序列化:
[DataMember]
public Collection<BaseClass> ClassesCollection { get; set; }
然后是示例请求的样子:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<GetDataUsingDataContract xmlns="http://tempuri.org/">
<composite xmlns:a="http://schemas.datacontract.org/2004/07/WcfService1" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<a:ClassesCollection>
<a:BaseClass i:type="a:ClassA">
<a:Id>1</a:Id>
<a:Name>5</a:Name>
</a:BaseClass>
</a:ClassesCollection>
</composite>
</GetDataUsingDataContract>
</s:Body>
</s:Envelope>