0

我有一个公开函数的 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>

4

2 回答 2

1

您是否尝试过ServiceKnownType属性?

MSDN这里有一篇博客文章很好地解释了它。

ServiceKnownType 继续实际的服务接口本身......

于 2013-02-07T16:39:06.960 回答
1

您必须将KnownType属性添加到合同中:

<DataContract(), KnownType(GetType(Cow)), KnownType(GetType(Dog))> _
Public Class Animal
   ...
End Class
于 2013-02-07T16:44:37.920 回答