我有一个WCF服务,它公开了一个函数:
Public Function FeedAnimal(ByVal animal As Animal) As Animal Implements IFarm.FeedAnimal
animal.Feed()
Return animal;
End Function
然而,Animal是一个具有许多派生类型的抽象类,每个派生类型都有自己的属性。然而,当我在SOAPUI中使用表示Cow的属性发出请求时,我会收到一个错误,即我无法创建抽象类。如何让WCF将参数反序列化为其实际类型?让我们假设它们都可以通过不同名称的属性来区分。
我已经尝试过在任何地方添加KnownType属性,但没有成功。这样做允许我返回派生类型,但不接受它们作为参数。
如果我想做的完全是疯狂的,请告诉我下一个最接近的事情
已编辑;如果我能让它只返回到用于此函数的XmlSerializer,我会很满意,它曾在以前的版本中工作
[ServiceContract(Namespace:=Constants.SERVICE_NAMESPACE)]
[ServiceKnownType(GetType(Cow))]
Public Interface IFarm
[OperationContract()]
[ServiceKnownType(GetType(Cow))]
[Validation(CheckAssertions:=False, SchemaValidation:=True)]
Function FeedAnimal(ByVal animal As Animal) _
As Animal
End Interface
和类
[KnownType(GetType(Cow))]
Public MustInherit Class Animal
Public weight As Integer
End Class
Public Class Cow
Inherits Animal
Public mooPower As Double
End Class
请求:
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:v3="mycomapnyaddresss" xmlns:foo="animalnamespace">
<soap:Header/>
<soap:Body>
<v3:FeedAnimal>
<!--Optional:-->
<v3:animal>
<!--Optional:-->
<foo:weight>100</foo:weight>
<foo:mooPower>10.0</foo:mooPower>
</v3:animal>
</v3:FeedAnimal>
您尝试过ServiceKnownType属性吗?
MSDN上有一篇博客文章对此进行了很好的解释。
ServiceKnowType在实际的服务接口本身上。。。
您必须将KnowType属性添加到合同中:
<DataContract(), KnownType(GetType(Cow)), KnownType(GetType(Dog))> _
Public Class Animal
...
End Class