简体   繁体   English

WCF服务已知类型对象将不返回已知类型的属性

[英]WCF Service known type object won't return properties of the known type

I have created a WCF service and have two particular objects, Contact and Driver. 我创建了WCF服务,并具有两个特定的对象,即Contact和Driver。 Driver inherits Contact. 驱动程序继承了Contact。 The Contact object is part of a larger Member object. Contact对象是较大的Member对象的一部分。 In the code I have the following: 在代码中,我有以下内容:

<DataContract()>
Public Class Contact
    <DataMember()>
    Public Name As String
    <DataMember()>
    Public HomePhone As String
    ... etc.
End Class

<DataContract()>
Public Class Driver
    Inherits Contact
    <DataMember()>
    Public LicenseNum As String
    <DataMember()>
    Public LicenseState As String
    ... etc.
End Class

<DataContract(), KnownType(GetType(Driver))>
Public Class Member
    <DataMember()>
    Public Info As Contact
    <DataMember()>
    Public PrimaryStore As Store
    ... etc.
End Class

Because I have the knowntype on the datacontract line, I can create a Member object with the Info property as a Driver instead of a Contact so I can save their license information. 因为我在数据合同行上具有knowntype,所以可以创建一个Info属性作为Driver而不是Contact的Member对象,以便保存其许可证信息。

My problem is that when I create a test project to see the returned data, I can't get the license information when I call the Info property. 我的问题是,当我创建一个测试项目以查看返回的数据时,调用Info属性时无法获得许可证信息。

Dim svc as New Service.RetrieveService
Dim sMember as Member
Dim memNum as String = "ABC123"
sMember = svc.GetMember(memNum)
Console.WriteLine("LicenseNumber" & sMember.Info...PROBLEM!!!)

sMember.Info only gives me properties belonging to Contact (Name, HomePhone, etc.) and none belonging to Driver (LicenseNum, LicenseState, etc.) How can I access this info? sMember.Info仅给我提供属于联系人的属性(名称,家庭电话等),而没有给我提供驱动程序的属性(LicenseNum,LicenseState等)。如何访问此信息?

I have already tried to add ServiceKnownType to my OperationContract as below: 我已经尝试将ServiceKnownType添加到我的OperationContract中,如下所示:

<ServiceContract()>
Public Interface RetrieveMember
    <OperationContract(), ServiceKnownType(GetType(Driver))>
    Function GetMember(ByVal memberNumber as String) As Member
End Interface

but it did not help. 但这没有帮助。

That is because the Member.Info property is defined as the Contact type. 这是因为Member.Info属性被定义为Contact类型。 That means, even if it references a Driver object, it will still be cast as a Contact object, so only those base members will be visible. 这意味着,即使它引用了Driver对象,它仍将被转换为Contact对象,因此只有那些基本成员才可见。 This is a problem which is unrelated to WCF. 这是与WCF无关的问题。 For instance, even removing WCF from the situation, you'd still have the same problem: 例如,即使从情境中删除WCF,您仍然会遇到相同的问题:

Dim m As New Member()
m.Info = New Driver()
Console.WriteLine(m.Contact.LicenseNum)  ' Won't compile!

So, if the Info property is referencing a Driver object, you must cast it to that type before you can access any of the Driver members: 因此,如果Info属性引用了Driver对象,则必须将其强制转换为该类型,然后才能访问任何Driver成员:

Dim m As Member
' ...
If TypeOf m.Info Is Driver Then
    Dim d As Driver = CType(m.Info, Driver)
    Console.WriteLine(d.LicenseNum)  ' Works fine
End if

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM